MKOverlayRenderer not rendering - ios

I'm having trouble getting an MKPolygonRenderer to appear over my map. I have a class MapViewController that contains an MKMapView, and create CustomMapOverlay instances to render over the MKMapView.
MapViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
self.mapView.delegate = self;
self.mapView.showsUserLocation = YES;
}
// ...
// Later, I retrieve some models and generate CustomMapOverlay instances from them...
for (Model *model in models) {
CustomMapOverlay *customMapOverlay = [[CustomMapOverlay alloc] initWithModel:model];
[self.mapView addOverlay:customMapOverlay];
}
// ...
// Implement MKMapViewDelegate protocol
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
MKPolygonRenderer *polygonRenderer = [[MKPolygonRenderer alloc] initWithOverlay:overlay];
polygonRenderer.lineWidth = 2;
polygonRenderer.strokeColor = [UIColor colorWithRed:0.0 green:0.5 blue:1.0 alpha:1.0];
polygonRenderer.fillColor = [UIColor colorWithRed:0.0 green:0.5 blue:1.0 alpha:0.5];
return polygonRenderer;
}
CustomMapOverlay.m:
#implementation CustomMapOverlay
// Required by MKOverlay protocol
#synthesize coordinate,
boundingMapRect;
- (instancetype)initWithModel:(Model *)model {
coordinate = CLLocationCoordinate2DMake(model.latitude, model.longitude);
double radiusInPoints = MKMapPointsPerMeterAtLatitude(model.latitude) * model.radius;
boundingMapRect = MKMapRectMake(model.latitude, model.longitude, radiusInPoints, radiusInPoints);
return self;
}
#end
mapView:rendererForOverlay is getting called, and inspecting the overlay in the debugger I see a coordinate within the map's current on-screen bounds and what seems like a reasonable boundingMapRect (though I'm not sure what "map points" are, I'm trusting that MKMapPointsPerMeterAtLatitude method to do what it says it does).
But, no polygons appear on the map.
UPDATE:
I realize now that I'm attempting to render polygons without creating them. Instead of CustomMapOverlay, then, I'm now generating MKPolygon overlays like this:
CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(model.latitude, model.longitude);
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(centerCoordinate, model.radius, model.radius);
int numCoords = 4;
CLLocationCoordinate2D *coords = malloc(sizeof(CLLocationCoordinate2D) * numCoords);
coords[0] = CLLocationCoordinate2DMake((region.center.longitude - 0.5*region.span.longitudeDelta), (region.center.latitude + 0.5*region.span.latitudeDelta));
coords[1] = CLLocationCoordinate2DMake((region.center.longitude + 0.5*region.span.longitudeDelta), (region.center.latitude + 0.5*region.span.latitudeDelta));
coords[2] = CLLocationCoordinate2DMake((region.center.longitude + 0.5*region.span.longitudeDelta), (region.center.latitude - 0.5*region.span.latitudeDelta));
coords[3] = CLLocationCoordinate2DMake((region.center.longitude - 0.5*region.span.longitudeDelta), (region.center.latitude - 0.5*region.span.latitudeDelta));
MKPolygon *polygon = [MKPolygon polygonWithCoordinates:coords count:numCoords];
free(coords);
[self.mapView addOverlay:polygon];
However, now mapView:rendererForOverlay is no longer getting called at all.

In the updated code which creates an MKPolygon, the coordinates in the coords array are backwards. For example, this line:
coords[0] = CLLocationCoordinate2DMake(
(region.center.longitude - 0.5*region.span.longitudeDelta),
(region.center.latitude + 0.5*region.span.latitudeDelta));
should be:
coords[0] = CLLocationCoordinate2DMake(
(region.center.latitude + 0.5*region.span.latitudeDelta,
(region.center.longitude - 0.5*region.span.longitudeDelta));
In the CLLocationCoordinate2DMake function, the first parameter is latitude, then longitude.
Because the coordinates are backwards, they may either be completely invalid or in the wrong location.
The rendererForOverlay delegate method will only get called if the overlay's boundingMapRect (which MKPolygon will automatically define based on the given coordinates) is in the currently displayed area of the map. But if the coordinates are invalid or in the wrong location, the boundingMapRect will be invalid as well.
By the way, in the original code which used CustomMapOverlay, there were at least these two issues:
The initWithModel method doesn't call [super init] (assuming it's an NSObject subclass).
The boundingMapRect is not calculated correctly. The MKMapRectMake function takes MKMapPoint values but the code is passing latitude and longitude in degrees. An MKMapPoint is not the same as a CLLocationCoordinate2D. You can convert a CLLocationCoordinate2D to an MKMapPoint using the MKMapPointForCoordinate function. See MKMapPointForCoordinate returning invalid coordinates and Map Coordinate Systems in the documentation for some more info.

Related

Why This Code Does Not Draw Polyline on MKMapView

I have the following code, with which i am trying to draw a polyline between a set of coordinates (which are correct as I also use them to add pins to the map, and those work fine).
I call a drawing method to initiate the drawing like so (the array in the method call contains the necessary coordinates):
[self drawRoute:[[transportData objectForKey:#"19"] objectForKey:#"stops"]];
This is the actual method that is supposed to draw the line on the map (selectedRoute is an MKPolyline object):
- (void)drawRoute:(NSArray *)routePointsArray {
if (selectedRoute) {
[mapView removeOverlay:selectedRoute];
selectedRoute = nil;
}
CLLocationCoordinate2D routeCoordinates[routePointsArray.count];
for (int i = 0; i < routePointsArray.count; i++) {
float latitude = [[[routePointsArray objectAtIndex:i] objectForKey:#"lat"] floatValue];
float longitude = [[[routePointsArray objectAtIndex:i] objectForKey:#"lon"] floatValue];
CLLocationCoordinate2D routePoint = CLLocationCoordinate2DMake(latitude, longitude);
routeCoordinates[i] = routePoint;
}
selectedRoute = [MKPolyline polylineWithCoordinates:routeCoordinates count:routePointsArray.count];
[mapView addOverlay:selectedRoute];
[mapView setVisibleMapRect:[selectedRoute boundingMapRect]];
}
And this is my delegate:
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
MKPolylineRenderer *routeLineView = [[MKPolylineRenderer alloc] initWithPolyline:selectedRoute];
if(overlay == selectedRoute)
{
if(nil == routeLineView)
{
routeLineView = [[MKPolylineRenderer alloc] initWithPolyline:selectedRoute];
routeLineView.fillColor = [UIColor redColor];
routeLineView.strokeColor = [UIColor redColor];
routeLineView.lineWidth = 5;
}
return routeLineView;
}
return nil;
}
I kind of narrowed it down to the routeCoordinates array not getting filled up with coordinates, but I do not understand why.
Also, if you spot any mistakes in the code I would really appreciate if you could point those out to me (possibly with a solution) as I am just learning this part of iOS and can use any help I can get.
You have an error in your rendererForOverlay method.
The first thing it does is assign an instance of MKPolylineRenderer to routeLineView, but later you only actually add the overlay if routeLineView is nil, which it won't be.
Remove the line that assigns the initial value to routeLineView.

Google maps iOS sdk get tapped overlay coordinates

i'm working with Google maps iOS sdk.
i want to get the coordinates of the touched point when user taps an overlay.
there is this delegate method:
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate
but it's not called when you tap an overlay or a marker.
can i call it programmatically (but coordinate parameter is missing-that's what i want..)?
or get location from this:
- (void) mapView: (GMSMapView *) mapView didTapOverlay: (GMSOverlay *) overlay
any suggestion's precious!
thanks!
UPDATE 6/2/15
Just staple a UITapGestureRecognizer onto the map and then extract the coordinate from the touch point. Your didTapAtCoordinate and didTapAtOverlay will continue to fire as before.
UITapGestureRecognizer *touchTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapTouchTap:)];
[self.view addGestureRecognizer:touchTap];
-(void)tapTouchTap:(UITapGestureRecognizer*)touchGesture
{
CGPoint point = [touchGesture locationInView:self.view];
CLLocationCoordinate2D coord = [self.mapView.projection coordinateForPoint:point];
NSLog(#"%f %f", coord.latitude, coord.longitude);
}
ORIGINAL POST
You are likely missing two snippets of code.
Adopt the GMSMapViewDelegate in your header file:
#interface Map : UIViewController <GMSMapViewDelegate>
You also need to set the delegate in your viewDidLoad:
self.mapView.delegate = self;
Now this should fire for you:
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate
{
NSLog(#"You tapped at %f,%f", coordinate.latitude, coordinate.longitude);
}
If you just want to get the position of the marker, there is a position property
CLLocationCoordinate2D coord = marker.position;
When this delegate method gets called
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate
its for screen taps that have no markers or overlays from my expierience.
GMSOverlay is a bit different because its a super class to a GMSMarker. You just need to subclass GMSOverlay for your custom overlays and add a position property. When you create the overlay, say in didTapAtCoordinate, you can assign the position (GPS coord) in there.
You could set the circle not tappable (default behaviour) and catch all the clicks on the map with the didTapAtCoordinate delegate.
Then when this event is triggered you could loop over all your circles to check if the user tapped inside one of the circles or outside.
self.mapview.settings.consumesGesturesInView = NO;
Add this line in viewdidload or after allocating.
And then implement this delegate method.
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate
{
NSLog(#"%g",coordinate);
}
- (void)mapView:(GMSMapView *)mapView didTapOverlay:(GMSOverlay *)overlay
{
GMSCircle *circle=(GMSCircle *)overlay;
CLLocationCoordinate2D touchCoOrdinate= circle.position;
NSLog(#"%g",touchCoOrdinate);
}
So you can't get the didTapAtCoordinateMethod to fire for an overlay tap. However I did find a slightly dirty workaround.
Using an overlay to draw polylines, we need a way to recognize where a polyline was tapped. So when drawing polylines we can build them like this.
//draw line
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.strokeColor = [UIColor purpleColor];
polyline.tappable = TRUE;
polyline.map = self.googleMapView;
polyline.title = routestring;
Where routestring is a built string from
routestring = [NSString stringWithFormat:#"%#/%#/%#",lat,lng,[annnotationobject objectForKey:#"linkId"]];
And lat and lng are string values of our coordinates. The last part is an ID for the polyline.
The routestring is storing the coordinates and an ID separated by '/' so that we can use component path of string to look them up later. This is assigned to the polylines title.
Now when the overlay is tapped:
-(void)mapView:(GMSMapView *)mapView didTapOverlay:(GMSOverlay *)overlay{
NSString *path = overlay.title;
//Finding componentpaths of string
NSArray *pathparts = [path pathComponents];
NSString *lat = [pathparts objectAtIndex:0];
NSString *lng = [pathparts objectAtIndex:1];
NSString *linkID = [pathparts objectAtIndex:2];
//Here we are building a marker to place near the users tap location on the polyline.
GMSMarker *marker = [GMSMarker markerWithPosition:CLLocationCoordinate2DMake([lat doubleValue],[lng doubleValue])];
marker.title = overlay.title;
marker.snippet = #"ROUTE DATA";
marker.map = self.googleMapView;
//This will popup a marker window
[self.googleMapView setSelectedMarker:marker];
}
We can use component paths of the string we built(separated by "/")to get the latitude and longitude coordinates from the polyline. Then assign them a marker to popup information on the overlay item.
You can subclass UITapGestureRecognizer and override its touchesEnded to retrieve the touch point, than retrieve the coordinate with GMSMapView's coordinateForPoint.
Subclass UITapGestureRecognizer and add it to your mapView:
self.touchTap = [[TouchGestureRecognizer alloc] initWithTarget:self action:#selector(tapTouchTap)];
self.touchTap.mapView = self.mapView;
[self.view addGestureRecognizer:self.touchTap];
TouchDownGestureRecognizer.h:
#import <UIKit/UIKit.h>
#interface TouchGestureRecognizer : UITapGestureRecognizer
#property GMSMapView *mapView;
#end
TouchDownGestureRecognizer.m:
#import "TouchGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
#implementation TouchGestureRecognizer
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
UITouch *touch = touches.allObjects[0];
CGPoint point = [touch locationInView:self.mapView];
CLLocationCoordinate2D coord = [self.mapView.projection coordinateForPoint:point];
NSLog(#"%f %f", coord.latitude, coord.longitude);
}
#end

MKPinAnnotationView color is not working

I am trying to show some pins from an array, it shows them all but they are red, and not green as i ask them to be.
Why is that ?
//run on array to get all locations
for(int k=0;k<[array count];k=k+2)
{
float targetlat=[[array objectAtIndex:k] floatValue];
float targetlongi=[[array objectAtIndex:k+1] floatValue];
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(targetlat,targetlongi);
NSString *partyTitle = #"title";
MKPinAnnotationView *partyPin = [self returnPointView:location andTitle:partyTitle andColor:MKPinAnnotationColorGreen];
[self.mapView addAnnotation:partyPin.annotation];
}
//function callback is working but its red, and it takes so much time to load
-(MKPinAnnotationView*) returnPointView: (CLLocationCoordinate2D) location andTitle: (NSString*) title andColor: (int) color
{
MKCoordinateRegion region = self.mapView.region;
region.center = location;
region.span.longitudeDelta /= 5.0;
region.span.latitudeDelta /= 5.0;
[self.mapView setRegion:region];
MKPointAnnotation *resultPin = [[MKPointAnnotation alloc] init];
MKPinAnnotationView *result = [[MKPinAnnotationView alloc] initWithAnnotation:resultPin reuseIdentifier:Nil];
[resultPin setCoordinate:location];
resultPin.title = title;
result.pinColor = color;
return result;
}
Regarding the main issue that the pins are red instead of green:
The code creates an MKPinAnnotationView but this view is never given to the map view.
To make the map view use annotation views that you create, you must implement the viewForAnnotation delegate method and return them from there.
Otherwise, the map view has no knowledge of annotation views that you create.
If you don't implement viewForAnnotation, the map view creates a default red pin view.
Regarding the second issue that "it takes so much time to load":
The most likely reason for this is that you are calling setRegion each time you add an annotation.
If you are adding, say, 500 annotations, the map view is setting the region 500 times.
Please note that it is not necessary to call setRegion simply to add an annotation (regardless of the currently-visible region). The annotation's coordinate does not have to be "visible" to add an annotation there.
What you want to do inside the for loop is simply construct a region that includes all the annotations and then call setRegion (or setVisibleRect) once and after all the annotations are added (after the for loop). Constructing an MKMapRect and calling setVisibleMapRect is easier than constructing an MKCoordinateRegion in order to call setRegion.
In iOS 7, this is even simpler: Just call showAnnotations (no manual construction necessary).
Example:
//Initialize the MKMapRect (region) we want to show to null...
MKMapRect showMapRect = MKMapRectNull;
for(int k=0;k<[array count];k=k+2)
{
float targetlat=[[array objectAtIndex:k] floatValue];
float targetlongi=[[array objectAtIndex:k+1] floatValue];
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(targetlat,targetlongi);
NSString *partyTitle = #"title";
//Here, don't create the annotation view.
//Just create the annotation...
MKPointAnnotation *resultPin = [[MKPointAnnotation alloc] init];
[resultPin setCoordinate:location];
resultPin.title = partyTitle;
[self.mapView addAnnotation:resultPin];
//Add this annotation's coordinate
//to the MKMapRect we want to show...
MKMapPoint annMapPoint = MKMapPointForCoordinate(location);
MKMapRect annMapRect = MKMapRectMake(annMapPoint.x, annMapPoint.y, 0, 0);
showMapRect = MKMapRectUnion(showMapRect, annMapRect);
}
mapView.visibleMapRect = showMapRect;
//In iOS 7, instead of constructing MKMapRect manually,
//we could just call showAnnotations...
//[mapView showAnnotations:mapView.annotations animated:YES];
//Implement the viewForAnnotation delegate method...
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
//if annotation is the user location,
//return nil so map view shows default view for it (blue dot)...
if ([annotation isKindOfClass:[MKUserLocation class]])
{
return nil;
}
static NSString *reuseId = #"pin";
MKPinAnnotationView *pav = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (pav == nil)
{
pav = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
pav.canShowCallout = YES;
pav.animatesDrop = YES;
pav.pinColor = MKPinAnnotationColorGreen;
}
else
{
pav.annotation = annotation;
}
return pav;
}

Incorrect user location shown on MKMapView after scaling the map programmatically

I have MKMapView which have UserTrackingMode = MKUserTrackingModeFollow,
and I have adding a circle overlay to show a region of the certain diameter at user location.
Also user can change the diameter of the region so I want to scale the map to insure whole region/circle is shown on that portion of the map.
The problem I have now is that scaling the map number of times by setting region results in incorrect user location annotation - it is moved from the correct location.
I cannot understand why is that happens, I see in the debugger that the mapView.userLocation property have correct coordinates.
But once new update is happens or I move the map manually - the annotation jumps to the correct place.
This is the code:
- (void)addCircleWithRadius:(double)radius andCoordinate:(CLLocationCoordinate2D)coordinate
{
_regionCircle = [MKCircle circleWithCenterCoordinate:coordinate radius:radius];
[_regionCircle setTitle:#"Region"];
MKCoordinateRegion region;
region.center.latitude = coordinate.latitude;
region.center.longitude = coordinate.longitude;
region.span.latitudeDelta = 0.00002 * _regionCircle.radius;
region.span.longitudeDelta = 0.00002 * _regionCircle.radius;
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits: region];
[self.mapView setRegion:adjustedRegion animated:TRUE];
_circleView = nil;
[self.mapView addOverlay:_regionCircle];
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
if(!_circleView)
{
_circleView = [[MKCircleView alloc] initWithCircle:overlay];
_circleView.strokeColor = [UIColor blueColor];;
_circleView.fillColor = [UIColor blueColor];
_circleView.alpha = 0.25;
_circleView.lineWidth = 2.0;
}
return _circleView;
}
- (IBAction)regionSliderValueChanged:(id)sender
{
[self updateRadiusCircle];
}
- (void) updateRadiusCircle
{
[self.mapView removeOverlays:self.mapView.overlays];
CLLocationCoordinate2D myCoordinate = {_currentLocation.coordinate.latitude, _currentLocation.coordinate.longitude};
[self addCircleWithRadius:self.radiusSlider.value andCoordinate:myCoordinate];
}
I have published the video on YouTube to better understand the issue.
https://www.youtube.com/watch?v=474gdjkGwJA

How to overlay a circle on an iOS map

I've got a radius and a location.
This is how I'm trying to get the bounding rectangle of the circle.
- (MKMapRect)boundingMapRect{
CLLocationCoordinate2D tmp;
MKCoordinateSpan radiusSpan = MKCoordinateRegionMakeWithDistance(self.coordinate, 0, self.radius).span;
tmp.latitude = self.coordinate.latitude - radiusSpan.longitudeDelta;
tmp.longitude = self.coordinate.longitude - radiusSpanSpan.longitudeDelta;
MKMapPoint upperLeft = MKMapPointForCoordinate(tmp);
MKMapRect bounds = MKMapRectMake(upperLeft.x, upperLeft.y, self.radius * 2, self.radius * 2);
return bounds;
}
MKMapRectMake(...) seems to want width and height measured in Map points. How do I convert the radius to that?
In the end I'm rendering it like this:
MKMapRect theMapRect = [self.overlay boundingMapRect];
CGRect theRect = [self rectForMapRect:theMapRect];
CGContextAddEllipseInRect(ctx, theRect);
CGContextFillPath(ctx);
The radius doesn't seem to equal meters on the map in the end and also the distance doesn't seem to be measured correctly. How to do it right?
I would be really thankful for every hint.
You should be using MKCircle instead. Do something like:
CLLocationDistance fenceDistance = 300;
CLLocationCoordinate2D circleMiddlePoint = CLLocationCoordinate2DMake(yourLocation.latitude, yourLocation.longitude);
MKCircle *circle = [MKCircle circleWithCenterCoordinate:circleMiddlePoint radius:fenceDistance];
[yourMapView addOverlay: circle];
And adopt the MKMapViewDelegate Method below and do something like this:
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
MKCircleView *circleView = [[[MKCircleView alloc] initWithCircle:(MKCircle *)overlay] autorelease];
circleView.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.9];
return circleView;
}
For iOS7 :
Same as tdevoy in viewDidLoad :
CLLocationDistance fenceDistance = 300;
CLLocationCoordinate2D circleMiddlePoint = CLLocationCoordinate2DMake(yourLocation.latitude, yourLocation.longitude);
MKCircle *circle = [MKCircle circleWithCenterCoordinate:circleMiddlePoint radius:fenceDistance];
[yourMapView addOverlay: circle];
But the delegate method (because mapView:viewForOverlay: is deprecated) :
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay
{
MKCircleRenderer *circleR = [[MKCircleRenderer alloc] initWithCircle:(MKCircle *)overlay];
circleR.fillColor = [UIColor greenColor];
return circleR;
}

Resources