2011-08-01 12 views
-4

iPhoneでGPSの位置を取得する方法を教えてください。iPhoneでGPSの位置を取得する方法

ありがとうございました。

+9

あなたが尋ねる前に、Appleのドキュメントを検索するためにも、最小限の試みを行う必要があります質問。そして、この質問は非常に一般的です。 – jtbandes

答えて

13

CLLocationManagerDelegateを実装し、この関数を記述し、これをviewDidLoadのviewControllerのメソッドで呼び出します。

-(void)startLocationManager 
{ 
    CLLocationManager *locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate=self; 
    locationManager.desiredAccuracy=kCLLocationAccuracyBestForNavigation; 
    [locationManager startUpdatingLocation]; 
} 

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{  
    CLLocationCoordinate2D coordinate = [newLocation coordinate]; 
    double dblLatitude = coordinate.latitude; 
    double dblLongitude = coordinate.longitude;  
} 
+1

ありがとうございますiOS 6で –

+0

でしたでした.UpdateToLocationは廃止されましたので、代わりに** didUpdateLocations **を使用してください – swiftBoy

4

CLLocationmanagerを使用している場合は、サービスが最初に有効になっているかどうかを確認してください。あなたがのMapViewを使用している場合

if ([CLLocationManager locationServicesEnabled]) 
{ 
    CLLocationManager *locationManager = [[CLLocationManager alloc] init]; 
    [locationManager startUpdatingLocation]; 

    CLLocationCoordinate2D *currentLocation = locationManager.location; 

    [locationManager stopUpdatingLocation]; 
    [locationManager release]; 
} 

、あなたが使用して現在位置を得ることができます:私は、現在の位置を取得するためにこれを行うだろう

[mapView setShowsUserLocation:YES]; 
CLLocationCoordinate2D *currentLocation = mapView.userLocation.coordinate; 
関連する問題