调用地图并带有导航功能的两种最简单方式

来源:互联网 发布:集体智慧编程 知乎 编辑:程序博客网 时间:2024/05/22 16:44

第一种方法先自定义一个webview(我用的XIB直接拖一个webview用iboutlet链接)然后

- (void)viewDidLoad

{

[palceWebloadRequest:[NSURLRequestrequestWithURL:[NSURLURLWithString:[NSStringstringWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f",

34.755806

,

113.673187

,

35.055806

,

114.073187

]]]];

}

就可以直接显示路线并支持自驾、公交、步行三种方式的路线查询同时可以实现导航(如果想要实现跳转页面的效果可以作如下操作)

新建一个controller然后调用presentViewController:beemVC方法即可代码如下

    BeemViewController *beemVC=[[BeemViewController alloc]initWithNibName:@"BeemViewController" bundle:nil];

    beemVC.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;

    void(^task)()=^{

        NSLog(@"这个页面是=====%@",self);

    };

    beemVC.homeLat=home.latitude;

    beemVC.homeLon=home.longitude;

    beemVC.officeLat=office.latitude;

    beemVC.officeLon=office.longitude;

     //跳转页面将地图放到webview上显示并适时导航

    [self presentViewController:beemVC animated:YES completion:task];



第二种方法 

摘自网上的调用本机自带的google地图有个弊端(调用地图后无法返回到自己的程序软件下)代码如下

//定义目的地CLLocationCoordinate2D类型

    CLLocationCoordinate2D ofice;

    ofice.latitude =office.latitude;

    ofice.longitude =office.longitude;

    //调用google地图定位

   MKMapItem *currentLocation = [MKMapItemmapItemForCurrentLocation];

    //调用google地图完成导航

    MKMapItem *toLocation = [[MKMapItemalloc] initWithPlacemark:[[MKPlacemarkalloc] initWithCoordinate:oficeaddressDictionary:nil]];

    toLocation.name =@"当前位置";

    [MKMapItemopenMapsWithItems:[NSArrayarrayWithObjects:currentLocation, toLocation, nil]

     launchOptions:[NSDictionarydictionaryWithObjects:[NSArrayarrayWithObjects:MKLaunchOptionsDirectionsModeDriving, [NSNumbernumberWithBool:YES],nil]forKeys:[NSArrayarrayWithObjects:MKLaunchOptionsDirectionsModeKey,MKLaunchOptionsShowsTrafficKey, nil]]];

-----------------------------------------------------------下面二图为第一种方式的效果图





原创粉丝点击