So verwende ich die Richtungen API in meiner App. Wenn ich die Polylinie aus der Antwort decodiere, wird es jedoch nicht korrekt angezeigt. Hier ist mein Code:Google Maps decodiert GMSPath inkorrekt iOS
//Construct request URL
NSString *urlString = [NSString stringWithFormat:
@"%@?origin=%f,%f&destination=%f,%f&sensor=true&key=%@",
@"https://maps.googleapis.com/maps/api/directions/json",
userMarker.position.latitude,
userMarker.position.longitude,
place.coordinate.latitude,
place.coordinate.longitude,
@"AIzaSyDrtHA-AMiVVylUPcp46_Vf1eZJJFBwRCY"];
NSURL *directionsURL = [NSURL URLWithString:urlString];
//Get directions in JSON format
dispatch_async(dispatch_get_main_queue(), ^{
NSData* data = [NSData dataWithContentsOfURL:directionsURL];
NSError* error;
if(data){
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
//Parse JSON and plot route on map
NSDictionary *routes = [json objectForKey:@"routes"][0];
NSDictionary *route = [routes objectForKey:@"overview_polyline"];
NSString *overview_route = [route objectForKey:@"points"];
//Clear map from previous polylines
[self.mapView clear];
//Make polyline
GMSPath *path = [GMSPath pathFromEncodedPath:overview_route];
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.strokeWidth = 4;
polyline.strokeColor = [UIColor darkGrayColor];
polyline.map = self.mapView;
});
Und hier ist der Linienzug:
Wie Sie finden es nicht die Straße richtig folgen. Mir scheint jedoch, dass es nicht genug Punkte gibt, um richtige Kurven zu haben.
EDIT: Dies geschieht nur in 50% der Zeit, manchmal zeigt es sich richtig, manchmal nicht.
Was könnte ich falsch machen?
Danke, aber wie funktioniert es in 50% der Zeit? –
Ich vermute, dass overview_polyline nur auf eine vereinfachte Version des Pfades eingestellt ist, wenn der vollständige Pfad zu kompliziert ist (zB über eine bestimmte Anzahl von Punkten). –
Ich denke du hast Recht! Vielen Dank! Wie Google Docs feststellt: Ich nehme an, Sie haben Recht! Vielen Dank! Google Docs gibt Folgendes an: "overview_polyline enthält ein einzelnes Punktobjekt, das eine codierte Polylinienrepräsentation der Route enthält. Diese Polylinie ist ein ungefährer (geglätteter) Pfad der resultierenden Richtungen." –