2011-12-22 89 views

回答

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; 

} 
0

你必須計算你自己的方式點和繪製路線作爲一個層。沒有公開的api在MKMapView中顯示路線。

最快的解決方案是打開手機的地圖應用程序與路線。

+0

是的,我知道這一點,但我在某些應用程序中看到可以在兩個位置之間繪製路線,只是不知道如何,我不需要指示此功能,因爲該應用程序將啓動地圖應用程序,我只是想要地圖繪製在地圖上的路線,我找不到任何如何去,但它可能 – 1337code

2

我嘗試下面的代碼......它工作得很好,我的項目...試試吧..

首先下載KMLParser.h和KMLParser.m從this鏈接: -

還可以下載DDAnnotation.h和DDAnnotation.m文件FR om this鏈接: -

現在創建一個像SomeViewController一樣的視圖控制器。

添加下列庫: -

  1. CoreLocation.framework
  2. MapKit.framework
  3. QuartzCore.framework

在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和文件的所有者給人以MapView的鏈接它。還要將MapView的委託設置爲文件的所有者。

現在SomeViewController收件以下代碼: - [上的MKMapView繪製航線]的

#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; 
    [email protected]"You are here"; 
    [email protected]"Current location"; 

    [mapView addAnnotation:myAnnotation]; 
    [annotations addObject:myAnnotation]; 

    CLLocationCoordinate2D theCoordinate1; 
    theCoordinate1.latitude = desiredLatitude; 
    theCoordinate1.longitude = desiredLongitude; 

    DDAnnotation* myAnnotation1=[[DDAnnotation alloc] init]; 

    myAnnotation1.coordinate=theCoordinate1; 
    [email protected]"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; 
    } 
} 
+0

無法給方法kml = [[KMLParser parseKMLAtURL:[....];給警告作爲foundClass方法'+ parseKMLAtURL:'找不到(返回類型默認爲'id') –

+0

您是否將以下鏈接中的KMLParser.h和KMLParser.m類文件添加到您的項目中? http://developer.apple.com/library/ios/#samplecode/KMLViewer/Listings/Classes_KMLParser_m.html#//apple_ref/doc/uid/DTS40010046-Classes_KMLParser_m-DontLinkElementID_4 –

+0

是在我的項目中添加了這些文件。但不工作。 –

相關問題