iOS App通過URL調用谷歌地圖導航

1、給iPhone安裝google map app,因為之後會使用這個程序進行導航

2、在自己的App中需要進行導航的地方,加入下列代碼

if ([[UIApplication sharedApplication] canOpenURL:
     [NSURL URLWithString:@"comgooglemaps://"]]) {
  [[UIApplication sharedApplication] openURL:
   [NSURL URLWithString:@"comgooglemaps://?center=40.765819,-73.975866&zoom=14&views=traffic"]];
} else {
  NSLog(@"Can't use comgooglemaps://");
}

這裏的if主要就是判斷手機中是否有google map app.如果有就將url參數傳入google mao app.

舉例:

comgooglemaps://?center=46.414382,10.013988&mapmode=streetview
center=46.414382,10.013988為中心點顯示地圖

3、導航的參數

實現導航功能的URL會有三個參數,saddr,daddr,directionsmode,以下是google官方提供的介紹。

  • saddr: Sets the starting point for directions searches. This can be a latitude,longitude or a query formatted address. If it is a query string that returns more than one result, the first result will be selected. If the value is left blank, then the user’s current location will be used.
  • daddr: Sets the end point for directions searches. Has the same format and behavior as saddr.
  • directionsmode: Method of transportation. Can be set to: driving, transit, bicycling or walking.

下面以例子來介紹該如何使用

1、官方例子,直接進行兩個點的導航
comgooglemaps://?saddr=Google+Inc,+8th+Avenue,+New+York,+NY&daddr=John+F.+Kennedy+International+Airport,+Van+Wyck+Expressway,+Jamaica,+New+York&directionsmode=transit
2、通過經緯度實現兩個點的導航
url = [NSString stringWithFormat:@"comgooglemaps://?saddr=37.782652,-122.410126&daddr=%lf,%lf&directionsmode=driving", targetLocation.latitude, targetLocation.longitude];
3、以手機所在位置為起始點進行導航
url = [NSString stringWithFormat:@"comgooglemaps://?daddr=%lf,%lf&directionsmode=driving", targetLocation.latitude, targetLocation.longitude];
原文地址:https://www.cnblogs.com/scaptain/p/4209141.html