2013-03-22 29 views
6

Mục đích của tôi là mở ứng dụng bản đồ từ ứng dụng ios với chỉ đường, tôi có thể mở ứng dụng bản đồ nhưng không hiển thị chỉ đường, tôi đã viết mã sauCách mở ứng dụng bản đồ táo theo hướng dẫn từ ứng dụng ios

NSString *mystr=[[NSString alloc] initWithFormat:@"http://maps.apple.com/maps?saddr=Current+Location&daddr=Newyork"]; 
      NSURL *myurl=[[NSURL alloc] initWithString:mystr]; 
      [[UIApplication sharedApplication] openURL:myurl]; 

Có thể ai giúp tôi tìm hiểu cách chuyển thông số cho url này và bất kỳ cách nào khác không?

+0

hãy nhìn vào liên kết này http://stackoverflow.com/questions/7605879/iphone-app-show-direction-using-map – Vinodh

Trả lời

11

Nếu bạn có nghĩa là dùng người dùng đến ứng dụng bản đồ dựa trên hai điểm, sau đó bạn có thể làm điều đó như thế này:

Tạo một NSURL trông như thế này:

NSURL *URL = [NSURL URLWithString:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f"]; 

Bạn cắm của bạn địa chỉ bắt đầu và đích (trong lat. và long.) một cách thích hợp. Yêu cầu ứng dụng của bạn mở URL

[[UIApplication sharedApplication] openURL:URL]; 

Nó sẽ đưa bạn đến ứng dụng bản đồ tự động!

+1

thanq rất nhiều vinodh –

+1

Perfect câu trả lời với những đường nét tối thiểu của mã. –

22
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(self.location.latitude,self.location.longitude); 

//create MKMapItem out of coordinates 
MKPlacemark* placeMark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil]; 
MKMapItem* destination = [[MKMapItem alloc] initWithPlacemark:placeMark]; 
if([destination respondsToSelector:@selector(openInMapsWithLaunchOptions:)]) 
{ 
    //using iOS6 native maps app 
    if(_mode == 1) 
    { 
     [destination openInMapsWithLaunchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeWalking}]; 

    } 
    if(_mode == 2) 
    { 
     [destination openInMapsWithLaunchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving}]; 

    } 
    if(_mode == 3) 
    { 
     [destination openInMapsWithLaunchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeTransit}]; 

    } 

} else{ 

    //using iOS 5 which has the Google Maps application 
    NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=Current+Location&daddr=%f,%f", self.location.latitude, self.location.longitude]; 
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]]; 
} 
+3

Biến '_mode' đến từ đâu? – SimplGy

+0

'_mode' ????? – Vvk

+0

Biến của riêng bạn để chọn những gì bạn muốn :) – nerowolfe

Các vấn đề liên quan