我希望实现一些功能,例如当我移动地图时,引脚也会移动并放置在我想要的位置并从用户的当前位置变为。
我试过下面的代码:
- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
答案 0 :(得分:1)
此代码在Swifty中,但在Obj-C中类似。 将手势识别器添加到mapView:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(mapViewTapped))
mapView.addGestureRecognizer(tapGesture)
然后,当您触摸地图时,将其转换为mapView上的相对点,然后查询地图位置:
func mapViewTapped(gestureRecognizer: UIGestureRecognizer) {
let touchPoint = gestureRecognizer.location(in: mapView)
let coordinate = mapView.convert(touchPoint, toCoordinateFrom: mapView)
addPin(at: coordinate)
}
最后,将图钉添加到地图中:
func addPin(at coordiante: Coordinate) {
let newAnnotation = MKPointAnnotation()
newAnnotation.coordinate = coordiante
mapView.addAnnotation(newAnnotation)
}