gpt4 book ai didi

ios - 在 MKMapView 上放置一个图钉

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:10:27 24 4
gpt4 key购买 nike

iPhone 新手来自 Java。所以我在这个阶段的目标是允许用户在 map 上“放置一个图钉”。我的 map 初始化如下所示:

- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"your view did load, I'm going to initizlie the map by your location");
CLLocationCoordinate2D location = theMap.userLocation.coordinate;
NSLog(@"Location found from Map: %f %f",location.latitude,location.longitude);

MKCoordinateRegion region;
MKCoordinateSpan span;

NSLog(@"coordinates: %f %f",location.latitude,location.longitude);
if (TARGET_IPHONE_SIMULATOR) {
NSLog(@"You're using the simulator:");
location.latitude = 40.8761620;
location.longitude = -73.782596;
} else {
location.latitude = theMap.userLocation.location.coordinate.latitude;
location.longitude = theMap.userLocation.location.coordinate.longitude;
}

span.latitudeDelta = 0.001;
span.longitudeDelta = 0.002;

region.span = span;
region.center = location;

[theMap setRegion:region animated:YES];
[theMap regionThatFits:region];
[theMap setMapType:MKMapTypeSatellite];
[theMap setZoomEnabled:YES];
[theMap setScrollEnabled:YES];
[theMap setShowsUserLocation:YES];
}

对于我要求的掉针

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = nil;
if (annotation != theMap.userLocation) {
static NSString *defaultPinID = @"aPin";
pinView = (MKPinAnnotationView *)[theMap dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if (pinView == nil)
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
} else {
}
pinView.pinColor = MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
return pinView;
}

我不确定我是否完全理解此 map (theMap) 如何在 viewForAnnotation 中用于图钉?我的意思是,用户执行什么操作会激活 viewForAnnotation 方法?此代码不起作用,我不确定为什么。

我正在使用模拟器,所以我不确定是否有一个按钮我应该按下或 Alt 单击 它?

最佳答案

I'm not sure I fully understand how this map (theMap) works for pins in viewForAnnotation?

MKPinAnnotationView 只是另一种注释 View ——也就是说,您向 map 添加注释(符合 MKAnnotation 协议(protocol)的对象)。本地图想要显示注释时(可能是因为用户滚动 map 以便注释在 View 中),它会要求您提供一个 View 来表示注释。那时,您的 mapView:viewForAnnotation: 方法可以获取或创建一个 pin 注释 View 并返回它。用户不会直接执行任何操作来触发 mapView:viewForAnnotation:,除了滚动或缩放。

如果您希望用户能够放置一个图钉,那就是另一回事了。您需要提供一个 View (甚至可能是一个 MKPinAnnotationView),他们可以四处拖动。当他们表示他们想要放下图钉(可能是通过抬起手指)时,您将移除 View 并在该点添加适当的注释。 然后 map View 将通过调用其委托(delegate)的mapView:viewForAnnotation: 方法要求您提供一个 View 来表示注释。

This code doesn't work and I'm not sure why.

您是否在 map 上添加了任何注释?如果是这样,您是否正在查看 map 上应该显示它们的部分?

我猜您正在查看 animatesDrop 属性,并期望它执行整个用户定位交互。它不会那样做。将该属性设置为 YES 只会使图钉在 map 上显示时动画化。

关于ios - 在 MKMapView 上放置一个图钉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16154518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com