如何从我的IOS应用程序中打开包含路线的苹果地图应用(How to open a apple map

2019-08-17 20:39发布

我的目标是从打开的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];

任何一个可以帮我如何弄清楚如何将参数传递到这个网址和任何其他?

Answer 1:

如果你的意思是采取用户基于两点地图应用程序,那么你可以做这样的:

创建一个看起来像这样的NSURL:

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

您插入您的起始地址和目的地(以拉特和长。)适当地。 告诉你的应用程序中打开URL

[[UIApplication sharedApplication] openURL:URL];

这应该带你到地图应用程序自动!



Answer 2:

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


文章来源: How to open a apple maps application with directions from my ios application