2012-03-14 21 views
1

CLLocationCoordinate2Dを使用しているユーザーの現在の場所を取得しようとしています。値を書式のようにしたいCLLocationCoordinate2Dを使用して現在の場所を取得

CLLocationCoordinate2D start = {-28.078694,153.382844};

私は次のようにそれらを使用できるように:

NSString *urlAddress = [NSString 
     stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f", 
     start.latitude, start.longitude, 
     destination.latitude, destination.longitude]; 

を私は長い&緯度現在取得する

CLLocation *location = [[CLLocation alloc]init ]; 
CLLocationDegrees currentLatitude = location.coordinate.latitude; 
CLLocationDegrees currentLongitude = location.coordinate.longitude; 

を使用。

しかし、テストしようとすると両方とも0.000になっています。私はiPhone 4でテストしています。

サンプルコードがあれば、それは素晴らしいでしょう。

+0

あなたは 'MKMapView'を使用していますか? –

+0

どのように座標を取得していますか?これまでに試したことのコードを追加してください。 – deanWombourne

+0

CLLocation * location = [[CLLocation alloc] init]を使用しています。 CLLocationDegrees currentLatitude = location.coordinate.latitude; CLLocationDegrees currentLongitude = location.coordinate.longitude; – iOSDev

答えて

15

最初CoreLocationフレームワークを追加し、次のコードを使用して...そして、あなたのViewControllerは、緯度と長い取得AppDelegateで、サンプルコードのためhttp://www.edumobile.org/iphone/miscellaneous/how-to-get-current-latitude-and-longitude-in-iphone/

+3

'この例の[locationManager location]'は、 'nil'または非常に古い場所を返します。現在の場所を取得する最善の方法は、 'locationManager'がそのデリゲートにコールバックメッセージを送るのを待つことです。しかし、コールバックメッセージで渡された位置は古いかもしれないので、常に位置のタイムスタンプをチェックする必要があります。 –

+2

locationServicesEnabledは最新版では非推奨です。 – Rams

+0

私のために働いていない緯度と経度の値はこのようになります。0.00000 – vijay

2
CLLocationCoordinate2D location = [[[mapview userLocation] location] coordinate]; 
NSLog(@"Location found from Map: %f %f",location.latitude,location.longitude); 

ともmapview0せずにリンクを(参照してください

https://developer.apple.com/library/ios/#samplecode/LocateMe/Introduction/Intro.html

+0

私はマップビューを使用していません。スタートと目的地をGoogleマップに追加するだけです。そしてそれらをUIWebViewで表示します。私はmapviewなしでそれを得ることができますか? – iOSDev

+0

はい、あなたは 'MKMapView'なしで行くことができます。 UIWebViewを使う理由は分かりにくいです。 –

+0

サンプルコード? – iOSDev

2

あなたはユーザの位置を取得するためにCLLocationManagerを使用する必要があります。CLLocationは、それができる、唯一の場所を表すために使用されるオブジェクトのクラスです」

詳細については、Location Awareness ProgrammingのAppleドキュメントに従ってください。

+0

正確な緯度と経度を得るためのサンプルコード? – iOSDev

+0

[Location Awareness Programming](https://developer.apple.com)のAppleドキュメントをご覧ください。com/library/ios /#documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html)。 –

1

使用ロケーションマネージャを実装する必要があります.hは次の変数を宣言します。 AppDelegate.h.mファイルに次のコードを記述して、

CLLocationManagerDelegate delegate. 

CLLocationManager *locationManager; 
CLLocation *currentLocation; 

で実行します。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

    self.locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    [locationManager startUpdatingLocation]; 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 

    self.currentLocation = newLocation; 
    self.currentLat = newLocation.coordinate.latitude; 
    self.currentLong = newLocation.coordinate.longitude; 
} 
7

をこのチュートリアルに従うことをCLLocationManagerDelegate

-(void)findCurrentLocation 
{ 

    CLLocationManager *locationManager = [[CLLocationManager alloc] init]; 
    if ([locationManager locationServicesEnabled]) 
    { 
     locationManager.delegate = self; 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
     locationManager.distanceFilter = kCLDistanceFilterNone; 
     [locationManager startUpdatingLocation]; 
    } 


    CLLocation *location = [locationManager location]; 
    CLLocationCoordinate2D coordinate = [location coordinate]; 

    NSString *str=[[NSString alloc] initWithFormat:@" latitude:%f longitude:%f",coordinate.latitude,coordinate.longitude]; 
    NSLog(@"%@",str); 


} 
+1

self.locationManager = [[CLLocationManager alloc] init];そのラインに漏れがあります。 – Rams

0
-(void)findCurrentLocation 
{ 
    if ([CLLocationManager locationServicesEnabled]) 
    { 
     CLLocationManager *locationManager = [[CLLocationManager alloc] init]; 
     locationManager.delegate = self; 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
     locationManager.distanceFilter = kCLDistanceFilterNone; 
     [locationManager startUpdatingLocation]; 
    } 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { 
    [self setLocation:[[manager location] coordinate]]; 
} 
関連する問題