Search code examples
objective-ciosxcodemkmapviewcllocation

Overlay questions on MKMapView


I have setup a CLLocation object, I am adding MKCircle overlay. I want the radius variable of the MKCircle to be relative to two this:

  1. The size of the Map View
  2. The size of the location

So, how can I retrieve some sort of size value from a CLLocation? And even better, would it automatically scale once I interact/zoom in out with the map view? Here is an overview of my objective:

Add an overlay to the map view. The overlay will be at a CLLocation CLLocationCoordinate2D variable, but I want that overlay to fit the size of that location and then resize once I zoom in and out with the map, which I am pretty sure it should do automatically. I know that I need to define the MKCircle's radius variable, but then how should I set it? So, do you guys know how I can do this?

Thanks!

UPDATE:

CLLocation *org = [[CLLocation alloc] initWithLatitude:coordO.latitude longitude:coordO.longitude];
CLLocation *des = [[CLLocation alloc] initWithLatitude:coordD.latitude longitude:coordD.longitude];

MKCircle *myCircle = [MKCircle circleWithCenterCoordinate:coordO radius:org.horizontalAccuracy];
MKCircle *myCircle2 = [MKCircle circleWithCenterCoordinate:coordD radius:des.horizontalAccuracy];

if (myCircle2 && myCircle) {

    NSLog(@"destination and origin both valid");
}

[mapView addOverlays: [NSArray arrayWithObjects:myCircle, myCircle2, nil]];

[myCircle setTitle:@"Placeholder"];
[myCircle2 setTitle:@"Placeholder"];

[localOverlays addObjectsFromArray: [NSArray arrayWithObjects:myCircle, myCircle2, nil]];

This is all the code I use to add an overlay to the map view, although it does not work, the overlay (MKCircle which I presume is a circle) is not there/visible. I took a look at the HazardMap Apple Sample Code and it looks like they have created a few other classes for setup. Is this necessary, what is wrong with my code. Thanks!


Solution

  • The radius of a MKCircle is a CLLocationDistance, which represents a distance measurement in meters. The horizontal accuracy of a CLLocation is CLLocationAccuracy which represents the accuracy of a coordinate value in meters.

    So, all you need to do is:

    MKCircle *circle = [MKCircle circleWithCenterCoordinate:location.coordinate
                                                     radius:location.horizontalAccuracy];
    

    UPDATE: The overlays are not visible because you created the locations manually, so their horizontal accuracy is zero. You can create a CLLocation with a custom horizontal accuracy using the method -[CLLocation initWithCoordinate:altitude:horizontalAccuracy:verticalAccuracy:timestamp:]. Or you may define a minimum radius for the circles (for example 10 meters):

    CLLocationDistance radius = MAX(10, location.horizontalAccuracy);