Last active
November 4, 2022 11:43
-
-
Save ryanhanwu/4dbcdbdf384f5a3cca1f to your computer and use it in GitHub Desktop.
Calculate Google Map length in meters with zoom level in #Objective-C (converted from #JavaScript) #iOS #Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)mapView:(GMSMapView*)mapView idleAtCameraPosition:(GMSCameraPosition*)position | |
{ | |
CLLocationCoordinate2D topLeft = mapView.projection.visibleRegion.farLeft; | |
CLLocationCoordinate2D bottomLeft = mapView.projection.visibleRegion.nearLeft; | |
double lat = fabs(topLeft.latitude - bottomLeft.latitude); | |
double mpp = cos(lat * M_PI / 180) * 2 * M_PI * 6378137 / (256 * pow(2, mapView.camera.zoom)); | |
double distance = mpp * mapView.frame.size.width; | |
[[SearchManager shareInstance] distance: distance]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var metersPerPixel = Math.cos(lat * Math.PI/180) * 2 * Math.PI * 6378137 / (256 * Math.pow(2, zoom)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Swift answer for 2019