Thursday, September 6, 2012

Convert pixel length to latitude longitude difference iOS


//  1.  Get difference from center of map to top middle of map (200 pixel difference).
//  2.  Convert this pixel difference into difference of latitude
//  3.  Reset the mapView center point with the new offset
//Grab the pixel difference from the center of the mapview

CGPoint centerOfMap = CGPointMake(mapView.frame.size.width/2, mapView.frame.size.height/2);
CGPoint topMidOfMap = CGPointMake(centerOfMap.x, centerOfMap.y-200); //Step 1
//Now get difference of longitude and latitude
CLLocationCoordinate2D centerOfMapCoord = [mapView convertPoint:centerOfMap toCoordinateFromView:mapView];   //Step 2
CLLocationCoordinate2D topMidOfMapCoord = [mapView convertPoint:topMidOfMap toCoordinateFromView:mapView]; //Step 2
CGFloat latitudeDiff = topMidOfMapCoord.latitude - centerOfMapCoord.latitude; //Step 2
coord.latitude -= latitudeDiff;
[mapView setCenterCoordinate:coord zoomLevel:default_zoom animated:NO]; //Step 3

No comments:

Post a Comment