如何在MKMapView中设置PlaceMark位置? [英] How to set the PlaceMark position in MKMapView?

查看:71
本文介绍了如何在MKMapView中设置PlaceMark位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MKMapView将图钉显示在mapView的中心,我想将图钉位置定位在中心上方.我该怎么办?

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?

这是我的代码

- (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];  
}

推荐答案

如果您希望将图钉保持在这些坐标上,但将地图的中心置于比图钉低的位置,以使图钉位于中心上方一点,则偏移region>的纬度跨度的一部分.

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.

代替此:

region.center=location;

执行以下操作:

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

增大/减小除数以减小/增大偏移量.

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


另外,不需要行[mapView regionThatFits:region];-您可以将其删除.
首先,setRegion已经完成了regionThatFits本身,其次,regionThatFits 返回了一个值,但是代码没有处理任何返回值.


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.

这篇关于如何在MKMapView中设置PlaceMark位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆