2009-10-10 88 views
18

我正在使用UIMapView在iPhone上顯示位置。我想做一個從當前位置到感興趣位置的指示,我不認爲它可能使用MapKit(但如果它是請通知),所以我會打開Goog​​le Maps應用程序或Safari瀏覽器來顯示它。我如何打開谷歌地圖在iPhone上使用座標的方向

我可以通過指定從(當前位置)到座標(感興趣的位置)的座標來做到這一點,我有這些經度和緯度。或者我必須使用街道地址?

如果我必須使用街道地址,我可以從經度和緯度得到它們。

回答

2

這是possible.Use 的MKMapView獲取的座標位置,你竊聽手機並用兩個座標要求從谷歌Web服務的KML文件,解析KML文件(示例應用程序KML觀衆在開發人員網站上)並顯示路線....
謝謝

+0

有機會獲得了一個示例項目? – 2011-06-29 08:03:51

+0

KML查看器項目將幫助您繪製兩個地方之間的路線。 – iGo 2011-06-29 11:34:39

74

是的,它不可能使用MapKit。您可以嘗試製作一份Google地圖網址請求,其中包含您當前的位置和目的地,這些地址和目的地將在Google地圖應用中隨路線打開。

下面是一個例子網址:

http://maps.google.com/?saddr=34.052222,-118.243611&daddr=37.322778,-122.031944

這裏是你如何可以在代碼中實現這一點:

CLLocationCoordinate2D start = { 34.052222, -118.243611 }; 
CLLocationCoordinate2D destination = { 37.322778, -122.031944 };  

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

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]]; 
+1

iOS6的地圖是一個笑話,順便說一句第一鏈路斷開時 – EaterOfCode 2012-09-26 12:18:09

+1

iOS6的發佈! :)任何人使用Apple Maps應用程序完成IOS6版本? – 2012-10-25 14:42:28

+0

有什麼網址可以在webview中打開我的意思是如果我們沒有谷歌地圖,你認爲它是一個好主意。 – dreamBegin 2018-02-01 06:19:48

1

堅實的解決方案是創建一個NIB的視圖控制器,包括UIWebView,然後傳遞執行Google地圖/定向服務的網址。這樣,您可以將用戶保留在應用程序中。這種方法在拉起網頁時是不夠的,因爲Apple套件不支持縮放。但對於OS4,至少用戶可以雙擊主頁按鈕並切換回應用程序。

-1

您可以通過電子郵件將已丟棄的PIN碼發送給自己,當您打開電子郵件中的鏈接時,它會顯示座標。

1

它可以顯示在MapKit路線:只要使用MKPolyline

我從googleMapsApi折線字符串。我用PHP解析它在服務器上,並返回最終polilyne字符串到我的應用程序。

NSMutableArray *points = [myApp decodePolyline:[route objectForKey:@"polyline"]]; 

if([points count] == 0) 
{ 
    return; 
} 

// while we create the route points, we will also be calculating the bounding box of our route 
// so we can easily zoom in on it. 
MKMapPoint northEastPoint; 
MKMapPoint southWestPoint; 

// create a c array of points. 
MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * [points count]); 

for(int idx = 0; idx < points.count; idx++) 
{ 
    // break the string down even further to latitude and longitude fields. 
    NSString* currentPointString = [points objectAtIndex:idx]; 
    NSArray* latLonArr = [currentPointString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]]; 

    CLLocationDegrees latitude = [[latLonArr objectAtIndex:0] doubleValue]; 
    CLLocationDegrees longitude = [[latLonArr objectAtIndex:1] doubleValue]; 

    // create our coordinate and add it to the correct spot in the array 
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude); 

    MKMapPoint point = MKMapPointForCoordinate(coordinate); 

    if (idx == 0) { 
     northEastPoint = point; 
     southWestPoint = point; 
    } 
    else 
    { 
     if (point.x > northEastPoint.x) 
      northEastPoint.x = point.x; 
     if(point.y > northEastPoint.y) 
      northEastPoint.y = point.y; 
     if (point.x < southWestPoint.x) 
      southWestPoint.x = point.x; 
     if (point.y < southWestPoint.y) 
      southWestPoint.y = point.y; 
    } 
    pointArr[idx] = point; 
    _currentLenght++; 
} 

// create the polyline based on the array of points. 
self.routeLine = [MKPolyline polylineWithPoints:pointArr count:points.count]; 

_routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, 
          northEastPoint.x - southWestPoint.x, 
          northEastPoint.y - southWestPoint.y); 

// clear the memory allocated earlier for the points 
free(pointArr); 

if (nil != self.routeLine) { 
     [self.mapView addOverlay:self.routeLine]; 
} 
[self.mapView setVisibleMapRect:_routeRect]; 

,並顯示:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay 
{ 
MKOverlayView* overlayView = nil; 

if(overlay == self.routeLine) 
{ 
    self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine] autorelease]; 
    self.routeLineView.fillColor = [UIColor blueColor]; 
    self.routeLineView.strokeColor = TICNavigatorColor; 
    self.routeLineView.lineWidth = 7; 
    self.routeLineView.lineJoin = kCGLineJoinRound; 
    self.routeLineView.lineCap = kCGLineCapRound; 

    overlayView = self.routeLineView; 
} 

return overlayView; 
} 

試試看吧。

+0

它只顯示線只從源到目的地..我想要顯示線像之字形。就像它顯示了一個合適的路線,你需要去int這個方向...... – Vivek2012 2013-01-24 10:32:26

4

使用在斯威夫特3谷歌和蘋果地圖婁代碼 -

if UIApplication.shared.canOpenURL(URL(string: "comgooglemaps://")!) 
     { 
      let urlString = "http://maps.google.com/?daddr=\(destinationLocation.latitude),\(destinationLocation.longitude)&directionsmode=driving" 

      // use bellow line for specific source location 

      //let urlString = "http://maps.google.com/?saddr=\(sourceLocation.latitude),\(sourceLocation.longitude)&daddr=\(destinationLocation.latitude),\(destinationLocation.longitude)&directionsmode=driving" 

      UIApplication.shared.openURL(URL(string: urlString)!) 
     } 
     else 
     { 
      //let urlString = "http://maps.apple.com/maps?saddr=\(sourceLocation.latitude),\(sourceLocation.longitude)&daddr=\(destinationLocation.latitude),\(destinationLocation.longitude)&dirflg=d" 
      let urlString = "http://maps.apple.com/maps?daddr=\(destinationLocation.latitude),\(destinationLocation.longitude)&dirflg=d" 

      UIApplication.shared.openURL(URL(string: urlString)!) 
     } 
1

首先檢查谷歌地圖被安裝在設備或不

if ([[UIApplication sharedApplication] canOpenURL: 
     [NSURL URLWithString:@"comgooglemaps://"]]) { 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"comgooglemaps://?saddr=23.0321,72.5252&daddr=22.9783,72.6002&zoom=14&views=traffic"]]; 
    } else { 
     NSLog(@"Can't use comgooglemaps://"); 
    } 

添加查詢模式英寸plist中

<key>LSApplicationQueriesSchemes</key> 
<array> 
<string>comgooglemaps</string> 
</array>