在iPhone MapView上显示当前位置和所需位置之间的路线

时间:2011-12-22 13:34:19

标签: iphone path mkmapview android-mapview routes

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

这样做的最佳方式是什么?

3 个答案:

答案 0 :(得分:8)

///在.h   添加委托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;

}

答案 1 :(得分:2)

我尝试使用以下代码...它在我的项目中运行良好...试试吧..

首先从this链接下载KMLParser.h和KMLParser.m: -

还从this链接下载DDAnnotation.h和DDAnnotation.m文件: -

现在创建一个像SomeViewController一样的视图控制器。

添加以下库: -

  1. CoreLocation.framework
  2. MapKit.framework
  3. QuartzCore.framework
  4. 在SomeViewController.h中导入KMLParser.h文件。

    在SomeViewController.h中编写以下代码

    #import <UIKit/UIKit.h>
    #import <MapKit/MapKit.h>
    #import "KMLParser.h"
    @interface SomeViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate, MKOverlay>{
    
        double currentLat;
        double currentLong;
    
        double desiredLatitude;
        double desiredLongitude;
    
        MKMapView *mapView;
        KMLParser *kml;
        NSMutableArray *annotations;
    
    } @property(nonatomic, retain)IBOutlet MKMapView *mapView;
    
    @end
    

    现在在SomeViewController.xib中拖放MapView并将其与来自File的所有者的mapView链接。还要将MapView的委托设置为File的所有者。

    现在在SomeViewController中编写以下代码: -

    #import "SomeViewController.h"
    #import <QuartzCore/QuartzCore.h>
    #import "DDAnnotation.h"
    
    @implementation SomeViewController
    @synthesize mapView;
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        currentLat = 21.215538; //You can set current latitude here. 
        currentLong = 72.858753; //You can set current longitude here.
    
        desiredLatitude = 21.211976; //You can set destination latitude here.
        desiredLongitude = 72.851593; //You can set destination longitude here.
    
        MKCoordinateRegion region = {{0.0f, 0.0f}, {100.0f, 100.0f}};
        CLLocation* currentLocation = [[CLLocation alloc] initWithLatitude:currentLat longitude:currentLong];
        region.center = currentLocation.coordinate;
        region.span.longitudeDelta = 0.05;
        region.span.latitudeDelta  = 0.05;
    
        [self.mapView setRegion:region animated:YES];
    
        annotations=[[NSMutableArray alloc] init];
    
        CLLocationCoordinate2D theCoordinate;
        theCoordinate.latitude = currentLat;
        theCoordinate.longitude = currentLong;
    
        DDAnnotation* myAnnotation=[[DDAnnotation alloc] init];
    
        myAnnotation.coordinate=theCoordinate;
        myAnnotation.title=@"You are here";
        myAnnotation.subtitle=@"Current location";
    
        [mapView addAnnotation:myAnnotation];
        [annotations addObject:myAnnotation];
    
        CLLocationCoordinate2D theCoordinate1;
        theCoordinate1.latitude = desiredLatitude;
        theCoordinate1.longitude = desiredLongitude;
    
        DDAnnotation* myAnnotation1=[[DDAnnotation alloc] init];
    
        myAnnotation1.coordinate=theCoordinate1;
        myAnnotation1.title=@"Desired Location's Title";
        myAnnotation1.subtitle= @"Desired Location's Sub-title";
    
        [mapView addAnnotation:myAnnotation1];
        [annotations addObject:myAnnotation1];
    
        NSString *path = [NSString stringWithFormat:@"http://maps.google.com/maps?f=d&hl=en&saddr=%f,%f&daddr=%f,%f&ie=UTF8&0&om=0&output=kml",currentLat,currentLong,desiredLatitude,desiredLongitude];
        NSLog(@" Path String : %@", path); 
        kml = [[KMLParser alloc] initWithURL:[NSURL URLWithString:path]];
        [kml parseKML];
    
        // Add all of the MKOverlay objects parsed from the KML file to the map.
        NSArray *overlays = [kml overlays];
        [self.mapView addOverlays:overlays];
    
    }
    
    
    #pragma mark MKMapViewDelegate
    
    - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
    {
        return [kml viewForOverlay:overlay];
    }
    
    - (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation{
    
        // if it's the user location, just return nil.
        if ([annotation isKindOfClass:[MKUserLocation class]]){
            return nil;
        }
        else{
    
            static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
            MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc]
                                             initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
            pinView.animatesDrop=YES;
            pinView.canShowCallout=YES;
            pinView.pinColor=MKPinAnnotationColorGreen;
    
    
            return pinView;
        }   
    }
    

答案 2 :(得分:0)

您必须以点的方式计算自己,并将路线绘制为图层。在MKMapView中显示路径不是公共API。

最快的解决方案是打开手机的地图应用程序和路线。