Comment charger une URL de file KML dans Google Maps à l'aide de l'API iOS?

J'ai Google Map embedded dans un controller de vue dans une carte sur un iPhone. Je peux créer ma carte en utilisant:

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; 

Maintenant, je veux append un file kml (à partir d'une URL) qui affiche un itinéraire. J'imagine qu'il y a quelque chose dans GMSMapView pour permettre cela soit comme calque ou autre chose, mais je n'ai pas de chance. J'ai vu le tutoriel KMS mais qui utilise un autre kit, MK quelque chose. De toute façon, y a-t-il un moyen de charger un file KML en utilisant l'API Google Maps for iOS?

Je sais que cette question date de plus d'un an mais je n'ai trouvé aucune solution, alors j'espère que ma solution sera utile.

Vous pouvez charger KML dans GMSMapView en utilisant iOS-KML-Framework . J'ai porté ce code à partir d'un projet utilisant KML-Viewer

Ajoutez une méthode pour parsingr le KML à partir de l'URL donnée, assurez-vous de transmettre le bon package d'applications à 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]; }); } 

Gérer le résultat ou l'erreur d'parsing 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) { __geomesortinges = __kml.geomesortinges; 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:NSLocalizedSsortingng(@"Error", nil) message:NSLocalizedSsortingng(@"Failed to read the KML file", nil) delegate:nil cancelButtonTitle:NSLocalizedSsortingng(@"OK", nil) otherButtonTitles:nil]; [alertView show]; }); } } 

Passez en revue les éléments de geometry de KML et ajoutez-les à GMSMapView en tant que marqueurs:

 - (void)reloadMapView { NSMutableArray *annotations = [NSMutableArray array]; for (KMLAbstractGeometry *geometry in __geomesortinges) { 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 = [NSSsortingng ssortingngWithFormat:@"%@", 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]; }); } 

À la fin de la dernière méthode, nous mettons à jour la vue de la camera pour l'adapter à tous les marqueurs ajoutés à la carte. Vous pouvez supprimer cette partie si ce n'est pas nécessaire.

KML n'est pas encore pris en charge dans le SDK. Veuillez déposer une request de fonctionnalité dans le suivi des problèmes .

C'est ainsi que j'ai résolu le même problème en utilisant 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; } } }