How to set the PlaceMark position in MKMapView?

佐手、 提交于 2019-12-10 23:10:47

问题


MKMapView show the pin on the center of the mapView, I want to position the pin location above the center. How do I do that?

Here is my code

- (void) location
{
    MKCoordinateRegion region;  
    MKCoordinateSpan span;  
    span.latitudeDelta = 0.1162;  
    span.longitudeDelta = 0.1160;  

    CLLocationCoordinate2D location;  
    location.latitude = 65.494806;  
    location.longitude = -23.577569;  
    region.span=span;  
    region.center=location;  

    PlaceMark *add_Annotation = [[[PlaceMark alloc] initWithCoordinate:location] autorelease];
    [add_Annotation setTitle:@"myTitle"];  
    [add_Annotation setSubTitle:@"myAddress"];  
    [mapView addAnnotation:add_Annotation]; 

    [mapView setRegion:region animated:TRUE];  
    [mapView regionThatFits:region];  
}

回答1:


If you want to keep the pin at those coordinates but center the map lower than the pin so that the pin is a little above the center, offset the center of the region by a fraction of the latitude span.

Instead of this:

region.center=location;

do this:

region.center = CLLocationCoordinate2DMake(
                    location.latitude - (span.latitudeDelta/5.0), 
                    location.longitude);

Increase/decrease the divisor to reduce/increase the offset.


Also, the line [mapView regionThatFits:region]; is not needed--you can remove it.
First, setRegion already does regionThatFits itself and second, regionThatFits returns a value but the code is not handling any return value.



来源:https://stackoverflow.com/questions/8171003/how-to-set-the-placemark-position-in-mkmapview

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