CoreLocation的当前位置

时间:2010-04-05 16:53:03

标签: iphone-sdk-3.0 google-maps

我有一个启动谷歌地图应用程序的应用程序。代码是:

UIApplication *app = [UIApplication sharedApplication]; 
[app openURL:[[NSURL alloc] initWithString: @"http://maps.google.com/maps?daddr=Obere+Laube,+Konstanz,+Germany&saddr="]]; 

saddr =应该是当前位置。我用

获取当前位置
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

NSLog(@"%f,%f", [newLocation coordinate]);

日志显示正确的坐标,如

2010-04-05 15:33:25.436 deBordeaux[60657:207] 37.331689,-122.030731

我找不到将坐标传输到url-string的正确方法。有人可以给我一个提示吗?


嗯,我的.h

中有条目

在我的方法中,我使用“newLocation”而不是“location”。代码是:

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

NSLog(@"%f,%f", [newLocation coordinate]);
NSLog(@"%f", [newLocation coordinate].latitude);

storedLocation = [newLocation coordinate];

NSLog(@"Standort neu String: %@", storedLocation);

结果我得到了:

  • 2010-04-05 20:28:44.397 deBordeaux [64179:207] 37.331689,-122.030731
  • 2010-04-05 20:28:44.398 deBordeaux [64179:207] 37.331689
  • 收到信号:“EXC_BAD_ACCESS”。

1 个答案:

答案 0 :(得分:0)

您不应使用网址的 addr 参数,但 ll

您应该将纬度和经度作为

传递
http://maps.google.com/maps?daddr=Obere+Laube,+Konstanz,+Germany&ll=37.331689,-122.030731

您可以从CLocation通过

获取它们
[location coordinate].latitude
[location coordinate].longitude

修改 首先,您可以直接存储CLLocationCoordinate2D类型的对象,然后在需要时将其转换为NSString:

// in your .h
CLLocationCoordinate2D storedLocation;

// in your method
storedLocation = [location coordinate];

// when you want to ask google maps
[NSString stringWithFormat:@"http://maps.google.com/maps?daddr=Obere+Laube,+Konstanz,+Germany&ll=%f,%f", storedLocation.latitude, storedLocation.longitude];

请注意CLLocationCoordinate2D被定义为

typedef struct {
   CLLocationDegrees latitude;
   CLLocationDegrees longitude;
} CLLocationCoordinate2D;

你有typedef double CLLocationDegrees