2010-10-07 139 views
0

一直在寻找这一点,但我似乎无法理解如何让用户当前的位置变成Google地图的URL,以打开Goog​​le地图应用程序,并填充指示。在Google地图openURL中使用位置管理器中的当前位置?

我已经从另一个教程中获取这段代码并将其添加到我的应用中。我需要从下面的“开始”lat/lng进入我的IBAction以打开Goog​​le地图应用程序。

- (void)locationUpdate:(CLLocation *)location { 
    // Lat/Lng of user (Needs to be sent to IBAction for btnDirections) 
    CLLocationCoordinate2D start = { location.coordinate.latitude, location.coordinate.longitude }; 
} 

以下是我的“获取路线”按钮的IBAction。

- (IBAction) onButtonClick:(id) sender { 
    if(sender == btnDirections) { 
     CLLocationCoordinate2D destination = { 52.48306771095311, -1.8935537338256836 };    
     NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f", 
             newLocation.coordinate.latitude, newLocation.coordinate.longitude, destination.latitude, destination.longitude]; 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]];  
    } 
} 

我已经在另一个应用程序之前这样做,但这种观点包含显示这让我得到纬度/经度容易的用户位置的地图。

在此先感谢。

回答

0

在我的代码,我有格式的URL:

http://maps.google.com/maps?daddr=%f,%f&saddr=%f,%f 

(这就是 '地图' 从你的加入)。

除此之外,您可以设置您的位置请求在设置精度后停止,您是否为您的目的设置了足够精确的位置?

哦,最后一点,获取当前位置可能无法在模拟器中工作。

+0

位置是在我的模拟器的工作。位置更新背后的想法是,如果人们朝着/远离目的地的方向行驶,它会打开Goog​​le地图,并在最近的位置不到一分钟前发出指示。 – 2010-10-07 15:36:31

0

您需要使用户的当前位置可用于onButtonClick:方法。您正在创建每个位置更新的开始结构,但您永远不会在该方法之外提供它。

1

只需通过“当前+位置”作为参数传递给谷歌地图应用程序:

http://maps.google.com/maps?saddr=Current+Location&daddr=Heathrow+Airport+London 
+0

它不适用于英语以外的设备语言,唉。 – 2011-04-28 22:00:40

+0

或在iPad上,即使是英文 – 2011-05-05 11:17:49

相关问题