Convert distance on MKMapView to distance for UIView

大兔子大兔子 提交于 2019-12-20 02:59:15

问题


How do I convert distance (for example 400 meters) on MKMapView to distance for UIView? I want to show MKAnnotationView that depends on current zoom level on MKMapView.


回答1:


First, create a region with 400 meters length:

MKCoordinateRegion myRegion = MKCoordinateRegionMakeWithDistance(mapView.centerCoordinate, 400, 0);

After that use convert region methods for getting real rect:

CGRect myRect = [mapView convertRegion: myRegion toRectToView: nil];
NSLog(@"width for 400m is %f", myRect.size.width);

Hope, this helps.




回答2:


You have several selectors in MKMapView to convert UIView coordinates on the screen to geo coordinates (and geo -> screen as well):

- (CGPoint)convertCoordinate:(CLLocationCoordinate2D)coordinate toPointToView:(UIView *)view
- (CLLocationCoordinate2D)convertPoint:(CGPoint)point toCoordinateFromView:(UIView *)view

I assume 400 meters is a distance between 2 coordinates, so should you have a reference to 2 CLLocationCoordinate2D structures.
Just use - (CGPoint)convertCoordinate:(CLLocationCoordinate2D)coordinate toPointToView:(UIView *)view to get the corresponding CGPoint (= screen coordinates) and you should be then able to calculate the screen distance.



来源:https://stackoverflow.com/questions/8412088/convert-distance-on-mkmapview-to-distance-for-uiview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!