Reputation: 8708
I have a problem using the Google Directions API. I'm getting the JSON string and parsing it, getting the overview_polylines string
NSString *allPolylines = [NSString stringWithFormat:[[[routes objectAtIndex:0] objectForKey:@"overview_polyline"] objectForKey:@"points"]];
This thing works. I'm having trouble adding it to the map. I'm using this post http://iosguy.com/tag/directions-api/ which seems to be exactly what I'm after.
I've implemented the
-(NSMutableArray *)decodePolyLine:(NSString *)encodedStr
method, and it works great. I NSLogged the results and it shows up as if I'm outputting CLLocation objects.
The problem is that I can't make it show up on the map. This is the code that follows
NSMutableArray *_path = [self decodePolyLine:allPolylines];
NSInteger numberOfSteps = _path.count;
CLLocationCoordinate2D coordinates[numberOfSteps];
for (NSInteger index = 0; index < numberOfSteps; index++) {
CLLocation *location = [_path objectAtIndex:index];
CLLocationCoordinate2D coordinate = location.coordinate;
coordinates[index] = coordinate;
}
MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
[_mapView addOverlay:polyLine];
[self mapView:_mapView viewForOverlay:polyLine];
I also tried adding an explicit call to
[self mapView:_mapView viewForOverlay:polyLine];
which is:
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
polylineView.strokeColor = [UIColor redColor];
polylineView.lineWidth = 1.0;
return polylineView;
}
The last method gets called, I checked using NSLog, but nothing shows up on the map.
Any thoughts? Thanks in advance!
Upvotes: 1
Views: 1225
Reputation:
The code shown looks OK except that you're not supposed to call viewForOverlay
directly.
The map view will call that delegate method when it needs to show the overlay.
The simplest reason it would not be calling the delegate method is that the map view's delegate
property is not set.
If the delegate
property is set, then verify that the coordinates are what you're expecting and also make sure they are not flipped (latitude in longitude and vice versa).
Another thing to check:
If _mapView
is an IBOutlet, make sure it is actually connected to the map view in the xib.
Upvotes: 1