Skip to content

Instantly share code, notes, and snippets.

@avioli
Created September 12, 2013 00:21
Show Gist options
  • Save avioli/6531668 to your computer and use it in GitHub Desktop.
Save avioli/6531668 to your computer and use it in GitHub Desktop.
When you've got a CLLocationCoordinate2D coordinate and want to plot a dot on a image of a map that you know its NW and SE coordinates. In my case the image is 100x100px.
CLLocationCoordinate2D sightingLocation = CLLocationCoordinate2DMake([self.sighting.locationLat floatValue], [self.sighting.locationLng floatValue]);
// Australia's map coordinates
CLLocationCoordinate2D nw, se;
nw = CLLocationCoordinate2DMake(-7.410849283839832, 112.41210912499992);
se = CLLocationCoordinate2DMake(-44.248667520681586, 154.07226537499992);
MKMapPoint topLeftPoint = MKMapPointForCoordinate(nw);
MKMapPoint bottomRightPoint = MKMapPointForCoordinate(se);
MKMapRect mapRect = MKMapRectMake(topLeftPoint.x, topLeftPoint.y, bottomRightPoint.x - topLeftPoint.x, bottomRightPoint.y - topLeftPoint.y);
MKMapPoint targetPoint = MKMapPointForCoordinate(sightingLocation);
BOOL isInside = MKMapRectContainsPoint(mapRect, targetPoint);
if (isInside)
{
double left = (targetPoint.x - topLeftPoint.x) / (bottomRightPoint.x - topLeftPoint.x);
double top = (targetPoint.y - topLeftPoint.y) / (bottomRightPoint.y - topLeftPoint.y);
CGFloat diameter = 10;
CGFloat radius = diameter / 2;
CGRect frame = CGRectMake(left * 100 - radius, top * 100 - radius, diameter, diameter);
UIView *circle = [[UIView alloc] initWithFrame:frame];
circle.alpha = 0.5;
circle.layer.cornerRadius = radius;
circle.backgroundColor = [UIColor IORedColor];
[self.mapImageView addSubview:circle];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment