6

我的目标是从带有方向的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];

谁能帮我弄清楚如何将参数传递给这个url和其他任何东西?

4

2 回答 2

23
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]];
}
于 2013-09-10T12:20:04.813 回答
11

如果您的意思是基于两点将用户带到地图应用程序,那么您可以这样做:

创建一个如下所示的 NSURL:

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

您适当地插入您的起始地址和目的地(经纬度)。告诉您的应用程序打开 URL

[[UIApplication sharedApplication] openURL:URL];

它应该会自动将您带到地图应用程序!

于 2013-03-22T07:18:40.953 回答