2009-10-10 53 views
18

Tôi đang sử dụng UIMapView để hiển thị vị trí trên iPhone. Tôi muốn làm một hướng từ vị trí hiện tại đến vị trí quan tâm, tôi không nghĩ rằng nó có thể sử dụng MapKit (nhưng nếu nó là xin vui lòng thông báo) Vì vậy, tôi sẽ mở một trong hai ứng dụng Google Maps hoặc safari để hiển thị nó.Làm cách nào để tôi mở Google Maps để tìm chỉ đường sử dụng tọa độ trên iphone

Tôi có thể làm điều này bằng cách chỉ định tọa độ từ (vị trí hiện tại) đến tọa độ (vị trí quan tâm) Tôi có các kinh độ và vĩ độ này. Hoặc tôi có phải sử dụng địa chỉ đường phố không?

Nếu tôi phải sử dụng địa chỉ đường phố, tôi có thể lấy chúng từ vĩ độ và kinh độ không.

Trả lời

2

Đó là possible.Use MKMapView Lấy tọa độ vị trí nơi bạn khai thác trên điện thoại và sử dụng hai tọa độ yêu cầu KML tập tin từ dịch vụ web google, phân tích KML tập tin (ví dụ ứng dụng KML xem trong trang web dành cho nhà phát triển) và hiển thị các tuyến đường ....
Cảm ơn bạn

+0

có dự án ví dụ nào không? –

+0

Dự án Trình xem KML sẽ giúp bạn vẽ tuyến đường giữa hai địa điểm. – iGo

74

Vâng, không thể sử dụng MapKit. Bạn có thể thử tạo một yêu cầu url của bản đồ Google chứa cả vị trí và đích đến hiện tại của bạn sẽ mở trong ứng dụng bản đồ của Google với chỉ đường.

Dưới đây là một url dụ:

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

Đây là cách bạn có thể thực hiện điều này trong mã của bạn:

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

Bản đồ iOS6 là một trò đùa, liên kết đầu tiên btw bị hỏng – EaterOfCode

+1

IOS6 được phát hành! :) Bất cứ ai đã thực hiện phiên bản IOS6 bằng ứng dụng Apple Maps cho điều này? –

+0

là có bất kỳ url để mở trong webview tôi có nghĩa là bạn nghĩ rằng nó là một ý tưởng tuyệt vời nếu chúng ta không có bản đồ google. – dreamBegin

1

Một giải pháp vững chắc là để tạo ra một bộ điều khiển vùng đang xem với một NIB bao gồm một UIWebView và sau đó chuyển URL thực hiện các dịch vụ bản đồ/hướng của Google. Bằng cách này, bạn giữ người dùng trong ứng dụng. Cách tiếp cận này là không đủ khi kéo lên một trang web, bởi vì bộ Apple không hỗ trợ phóng to. Nhưng với OS4, ít nhất người dùng có thể nhấp đúp vào nút trang chủ và chuyển về ứng dụng.

-1

Bạn có thể gửi mã pin đã bị rơi vào chính mình và khi bạn mở liên kết trong email, nó sẽ hiển thị tọa độ.

1

Có thể hiển thị tuyến đường trong MapKit: Chỉ cần sử dụng MKPolyline

tôi nhận được chuỗi polyline từ googleMapsApi. Tôi phân tích nó trên máy chủ với php, và trả lại chuỗi polilyne cuối cùng cho ứng dụng của tôi.

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]; 

Và hiển thị:

- (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; 
} 

Hãy thử.

+0

Dòng Chỉ Hiển thị của nó chỉ từ Nguồn đến Đích .. Tôi muốn Hiển thị dòng như Hình dạng Zigzag. Giống như nó cho thấy một lộ trình thích hợp mà U cần phải đi theo hướng này ... – Vivek2012

4

Sử dụng mã dưới đây cho cả bản đồ google và táo trong Swift 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

Đầu tiên bản đồ kiểm tra google được cài đặt trong thiết bị hoặc không

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://"); 
    } 

Thêm schema truy vấn trong.plist

<key>LSApplicationQueriesSchemes</key> 
<array> 
<string>comgooglemaps</string> 
</array> 
Các vấn đề liên quan