2013-06-25 54 views
2

我已經將Google Map嵌入到iPhone上的Map中的View Controller中。我可以創建我的地圖使用:如何使用iOS API將KML文件URL加載到Google地圖?

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:39.93 
                 longitude:-75.17 
                  zoom:12]; 
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera]; 

// use GPS to determine location of self 
mapView_.myLocationEnabled = YES; 
mapView_.settings.myLocationButton = YES; 
mapView_.settings.compassButton = YES; 

現在,我想添加一個顯示路線的kml文件(從URL)。我會想象GMSMapView中有一些東西可以作爲圖層或其他東西,但我沒有任何運氣。我見過KMS教程,但是使用了其他一些工具包,MK。無論如何,有沒有一種方法可以使用Google Maps for iOS API加載KML文件?

回答

3

我知道這個問題已經超過1年了,但我找不到任何解決方案,所以我希望我的解決方案將會有用。

您可以使用iOS-KML-Framework將KML加載到GMSMapView中。我是移植使用KML-Viewer

Add方法根據給定的URL解析KML從項目的代碼,請確保您傳遞正確的應用程式束dispatch_queue_create():

- (void)loadKMLAtURL:(NSURL *)url 
{ 
    dispatch_queue_t loadKmlQueue = dispatch_queue_create("com.example.app.kmlqueue", NULL); 

    dispatch_async(loadKmlQueue, ^{ 
     KMLRoot *newKml = [KMLParser parseKMLAtURL:url]; 

     [self performSelectorOnMainThread:@selector(kmlLoaded:) withObject:newKml waitUntilDone:YES]; 
    }); 
} 

處理的KML解析導致或錯誤:

- (void)kmlLoaded:(id)sender { 
    self.navigationController.view.userInteractionEnabled = NO; 

    __kml = sender; 

    // remove KML format error observer 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kKMLInvalidKMLFormatNotification object:nil]; 

    if (__kml) { 
     __geometries = __kml.geometries; 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      self.navigationController.view.userInteractionEnabled = YES; 

      [self reloadMapView]; 
     }); 
    } else { 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      self.navigationController.view.userInteractionEnabled = YES; 

      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", nil) 
                   message:NSLocalizedString(@"Failed to read the KML file", nil) 
                   delegate:nil 
                 cancelButtonTitle:NSLocalizedString(@"OK", nil) 
                 otherButtonTitles:nil]; 
      [alertView show]; 
     }); 
    } 
} 

走了過來,從KML幾何形狀的物品,並將它們添加到GMSMapView中作爲標記:

- (void)reloadMapView 
{ 
    NSMutableArray *annotations = [NSMutableArray array]; 

    for (KMLAbstractGeometry *geometry in __geometries) { 
     MKShape *mkShape = [geometry mapkitShape]; 
     if (mkShape) { 
      if ([mkShape isKindOfClass:[MKPointAnnotation class]]) { 
       MKPointAnnotation *annotation = (MKPointAnnotation*)mkShape; 

       GMSMarker *marker = [[GMSMarker alloc] init]; 
       marker.position = annotation.coordinate; 
       marker.appearAnimation = kGMSMarkerAnimationPop; 
       marker.icon = [UIImage imageNamed:@"marker"]; 
       marker.title = annotation.title; 
       marker.userData = [NSString stringWithFormat:@"%@", geometry.placemark.descriptionValue]; 
       marker.map = self.mapView; 

       [annotations addObject:annotation]; 
      } 
     } 
    } 

    // set bounds in next run loop. 
    dispatch_async(dispatch_get_main_queue(), ^{ 

     GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] init]; 

     for (id <MKAnnotation> annotation in annotations) 
     { 
      bounds = [bounds includingCoordinate:annotation.coordinate]; 
     } 

     GMSCameraUpdate *update = [GMSCameraUpdate fitBounds:bounds]; 
     [self.mapView moveCamera:update]; 
     [self.mapView animateToViewingAngle:50]; 
    }); 

} 

在最後一個方法結束時,我們使用updating the camera view來適應添加到地圖上的所有標記。如果不需要,可以移除這部分。

+0

你似乎做了很多更多的傳遞到主線程比嚴格要求... – Wain

+0

@Wain,你可能是對的。原來的代碼不是我的,所以我沒有機會審查它的效率。剛剛將它移植到Google Maps SDK昨天。 – f0xik

0

這就是我如何使用提到的iOS-KML-Framework解決類似的問題。

#import <GoogleMaps/GoogleMaps.h> 
#import "KML.h" 

@property (weak, nonatomic) IBOutlet GMSMapView *mapView; 

- (void)loadZonesFromURL:(NSURL *)url { 

KMLRoot* kml = [KMLParser parseKMLAtURL: url]; 

for (KMLPlacemark *placemark in kml.placemarks) { 
    GMSMutablePath *rect = [GMSMutablePath path]; 

    if ([placemark.geometry isKindOfClass:[KMLPolygon class]]) { 
     KMLLinearRing *ring = [(KMLPolygon *)placemark.geometry outerBoundaryIs]; 

     for (KMLCoordinate *coordinate in ring.coordinates) { 
      [rect addCoordinate:CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude)]; 
     } 

     GMSPolygon *polygon = [GMSPolygon polygonWithPath:rect]; 
     polygon.fillColor = [UIColor colorWithRed:67.0/255.0 green:172.0/255.0 blue:52.0/255.0 alpha:0.3]; 
     polygon.map = self.mapView; 

    } 

} 


} 
相關問題