我有一个GPS坐标(纬度,经度),我很快就想在显示该位置的MKMapView上放置一个引脚。一切都运行得很好,但因为我只需要一个没有标注的单个引脚是否有更快的方法来做到这一点,或者我在下面需要做什么?
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation {
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"DETAILPIN_ID"];
[pinView setAnimatesDrop:YES];
[pinView setCanShowCallout:NO];
return pinView;
}
注意:我不需要检查可重复使用的注释视图,因为我只使用引脚在详细视图中显示位置(在下次请求详细视图时销毁并重新创建)。
而不是使用-mapView:viewForAnnotation:
方法,只需将MKPointAnnotation
的代码放入-viewDidLoad
方法。它不会为掉落动画,但它很容易。
// Place a single pin
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:centerCoordinate];
[annotation setTitle:@"Title"]; //You can set the subtitle too
[self.mapView addAnnotation:annotation];
Swift版本:
let annotation = MKPointAnnotation()
let centerCoordinate = CLLocationCoordinate2D(latitude: 41, longitude:29)
annotation.coordinate = centerCoordinate
annotation.title = "Title"
mapView.addAnnotation(annotation)
您可以像这样设置点和区域:
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(lat, lon);
MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
MKCoordinateRegion region = {coord, span};
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:coord];
[self.staticMapView setRegion:region];
[self.staticMapView addAnnotation:annotation];
MKMapView中添加引脚的Swift版本:
func addPin() {
let annotation = MKPointAnnotation()
let centerCoordinate = CLLocationCoordinate2D(latitude: 20.836864, longitude:-156.874269)
annotation.coordinate = centerCoordinate
annotation.title = "Lanai, Hawaii"
mapView.addAnnotation(annotation)
}
Swift版本的聚焦区域或引脚
func focusMapView() {
let mapCenter = CLLocationCoordinate2DMake(20.836864, -156.874269)
let span = MKCoordinateSpanMake(0.1, 0.1)
let region = MKCoordinateRegionMake(mapCenter, span)
mapView.region = region
}
要了解更多详情,请访问“Load MKMapView using Swift”。
删除引脚并在地图视图中显示当前位置的最简单方法是在viewDidLoad方法中添加此代码。 (假设您已经获取了用户的当前位置)。
NSString *display_coordinates=[NSString stringWithFormat:@"Latitude is %f and Longitude is %f",coordinate.longitude,coordinate.latitude];
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:coordinate];
[annotation setTitle:@"Click Labs"];
[annotation setSubtitle:display_coordinates];
[mapView addAnnotation:annotation];
只需添加它就像:
mapView.mapType = MKMapType.standard
let location = CLLocationCoordinate2D(latitude: 23.0225,longitude: 72.5714)
let span = MKCoordinateSpanMake(0.05, 0.05)
let region = MKCoordinateRegion(center: location, span: span)
mapView.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = "Javed Multani"
annotation.subtitle = "Surat, India"
mapView.addAnnotation(annotation)
看起来像:
由于你想要为drop做动画,你需要像你一样实现viewForAnnotation
,因为默认情况下该属性是NO
。
如果您不需要为drop添加动画,则可以完全消除viewForAnnotation
方法实现并禁用callout,在添加注释时将注释的title
设置为nil或空白。
如果你确实需要pin drop动画并且你还要显示用户的位置(蓝点),你还需要检查MKUserLocation
中的viewForAnnotation
并返回nil
。
否则,您可以删除整个viewForAnnotation
方法,默认的红色图钉将显示没有动画,标注将显示title
不是空白,并且用户位置将显示为蓝点。