上传MKPinAnnotationView的自定义图像后,我注意到该针是偏心的。该引脚应位于路径折线上的一个点上,并位于mkcircle的中心;但是,针似乎位于折线的右侧,位于中心的北侧。我尝试过尝试使用centerOffset属性,但是当我将值插入属性时,似乎没有任何改变。这是代码,
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
static NSString *viewID = @"MKPinAnnotationView";
MKPinAnnotationView *annotationView = (MKPinAnnotationView*)
[self.mapView dequeueReusableAnnotationViewWithIdentifier:viewID];
if(annotationView ==nil){
annotationView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:viewID];
}
annotationView.image = [UIImage imageNamed:@"Pin.png"];
annotationView.enabled = YES;
//doesn't move the pin, still offcentered
annotationView.centerOffset = CGPointMake(0,-50);
return annotationView;
}
只需要添加一些内容,我还注意到,使用新的图钉图像时,单击图钉时不会弹出任何内容。之前,使用默认引脚,单击引脚后会出现一个文本气泡。既然如此,我想要包含制作方法的代码并将引脚放在地图上,
-(void) createAndAddAnnotationForCoordinate : (CLLocationCoordinate2D)coordinate{
MKPointAnnotation* annotation = [[MKPointAnnotation alloc]init];
annotation.coordinate = coordinate;
annotation.title = @"This is a pin!";
[mapView addAnnotation:annotation];
}
我还尝试更改图钉图像以查看是否会影响MKPinAnnotationView的定位。虽然我能够通过编辑图像使引脚居中,但它并不是以其他折线为中心。任何帮助将不胜感激!
答案 0 :(得分:9)
首先,重要的一点是,在使用自定义注释图像时,最好使用普通MKAnnotationView
类而不是其子类MKPinAnnotationView
,它旨在自动显示标准图像。
这是因为MKPinAnnotationView
包括注释视图框架和centerOffset的一些内置调整,基于它自己的图钉图像。在某些情况下,您的自定义图像甚至会在屏幕上替换为默认的图像。因此,即使MKPinAnnotationView
具有image
属性,该类也不会始终按预期使用它。
其次,设置centerOffset
使得当地图被缩放时,图像中的一部分&#34;指向&#34;到坐标保持指向坐标。这是因为centerOffset
位于屏幕CGPoint
中,并且不会随地图的缩放级别缩放。如果centerOffset
未正确设置,则&#34;指向&#34;图像的图像将开始从目标坐标漂移。
另请注意,您甚至可能不需要设置centerOffset
,因为默认设置会将图像的中心放在您可能正常的坐标上。
根据您发布的图片,以下是代码和结果外观 ,但未设置centerOffset
(保留默认设置) :
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
static NSString *viewID = @"MKPinAnnotationView";
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:viewID];
if (annotationView ==nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:viewID];
annotationView.image = [UIImage imageNamed:@"Pin.png"];
annotationView.canShowCallout = YES;
}
else {
annotationView.annotation = annotation;
}
return annotationView;
}
(我添加了红色中心线以显示目标坐标相对于图钉图像的位置。)
以下是设置centerOffset
的代码和结果外观 ,以便底部指向坐标 :
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
static NSString *viewID = @"MKPinAnnotationView";
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:viewID];
if (annotationView ==nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:viewID];
annotationView.image = [UIImage imageNamed:@"Pin.png"];
annotationView.canShowCallout = YES;
annotationView.centerOffset = CGPointMake(0,-15);
}
else {
annotationView.annotation = annotation;
}
return annotationView;
}
答案 1 :(得分:2)
你必须设置MKCoordinateRegion来加载地图,编辑你的createAndAddAnnotationForCoordinate方法,如下所示
-(void) createAndAddAnnotationForCoordinate : (CLLocationCoordinate2D)coordinate{
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(coordinate, 3000, 3000); //Set zooming level
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion]; //add location to map
[mapView setRegion:adjustedRegion animated:YES]; // create animation zooming
MKPointAnnotation* annotation = [[MKPointAnnotation alloc]init];
annotation.coordinate = coordinate;
annotation.title = @"This is a pin!";
[mapView addAnnotation:annotation];
}