在 iPhone MapView 上显示当前位置和所需位置之间的路线 [英] Show route between current and desired location on iPhone MapView

查看:20
本文介绍了在 iPhone MapView 上显示当前位置和所需位置之间的路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 MKMapView 上显示当前位置和所需位置之间的路线作为注释.

I want to show a route on a MKMapView between the current location and a desired location as an annotation.

最好的方法是什么?

推荐答案

////in .h添加委托 MKMapViewDelegate

///in .h add delegate MKMapViewDelegate

////在.m文件中

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation     *)newLocation fromLocation:(CLLocation *)oldLocation
{

     CLLocationCoordinate2D newcordinate =   newLocation.coordinate;
     CLLocationCoordinate2D oldcordinate =   oldLocation.coordinate;

       MKMapPoint * pointsArray =
               malloc(sizeof(CLLocationCoordinate2D)*2);

           pointsArray[0]= MKMapPointForCoordinate(oldcordinate); 
           pointsArray[1]= MKMapPointForCoordinate(newcordinate);    

            MKPolyline *  routeLine = [MKPolyline polylineWithPoints:pointsArray count:2];
           free(pointsArray);

            [MapView addOverlay:routeLine];  //MkMapView declared in .h
}

//MKMapViewDelegate

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    MKOverlayView* overlayView = nil;


          MKPolylineView  * _routeLineView = [[[MKPolylineView alloc]                   initWithPolyline:self.routeLine] autorelease];
            _routeLineView.fillColor = self.PathColor;
           _routeLineView.strokeColor = self.PathColor;
          _routeLineView.lineWidth = 15;
            _routeLineView.lineCap = kCGLineCapSquare;


            overlayView = _routeLineView;

            return overlayView;

}

这篇关于在 iPhone MapView 上显示当前位置和所需位置之间的路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆