Updating MKMapView to CLPlacemark returned from CLGeocoder

前端 未结 1 1103
小鲜肉
小鲜肉 2021-01-14 20:48

I want to be able to update the region displayed on a MKMapView by allowing the user to type in an address or location in a UIAlertView. I currently have:

           


        
相关标签:
1条回答
  • 2021-01-14 21:32

    You could just set it equal to latitudeDelta and the map view will adjust as needed.

    But you don't need to calculate the span yourself in the first place. You can use:

    region = MKCoordinateRegionMakeWithDistance(
                 placemark.region.center, 
                 placemark.region.radius, 
                 placemark.region.radius);
    

    Not sure about the second part of your question.


    In iOS 7 and higher, the region returned by the CLPlacemark is actually a CLCircularRegion (see Deprecated CLRegion methods - how to get radius?).

    Although the original code will still work as-is, you may get a compiler warning that radius and center are deprecated.

    To avoid the warning, cast the region as a CLCircularRegion:

    CLCircularRegion *pmCircularRegion = (CLCircularRegion *)placemark.region;
    
    region = MKCoordinateRegionMakeWithDistance(
             pmCircularRegion.center,
             pmCircularRegion.radius,
             pmCircularRegion.radius);
    
    0 讨论(0)
提交回复
热议问题