用户上的自定义Pin';使用MKMapView在iOS地图上的当前位置
我正在尝试在用户的当前位置设置pin。从用户上的自定义Pin';使用MKMapView在iOS地图上的当前位置,ios,objective-c,gps,mkmapview,Ios,Objective C,Gps,Mkmapview,我正在尝试在用户的当前位置设置pin。从locationmanager中拾取位置并将其分配给MKAnnotation。但当我运行应用程序时,地图会在地图上的不同位置显示用户的位置和Pin位置。不太远,但我想知道为什么不一样。其中,自定义pin和用户在模拟器上的位置相同 下面是一些示例代码 self.locationManager = [[CLLocationManager alloc] init]; [self.locationManager requestWhenInUseAuthorizat
locationmanager
中拾取位置并将其分配给MKAnnotation
。但当我运行应用程序时,地图会在地图上的不同位置显示用户的位置和Pin
位置。不太远,但我想知道为什么不一样。其中,自定义pin和用户在模拟器上的位置相同
下面是一些示例代码
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager startUpdatingLocation];
MyLocation *mylocation = [[MyLocation alloc] initWithName:@"name" address:@"address" coordinate:self.locationManager.location.coordinate] ;
我需要设置一些精度级别吗?想知道MKMapView是如何显示位置的。请尝试以下代码:
#pragma mark -
#pragma mark MKMapView delegate
- (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if(annotationView)
return annotationView;
else
{
MKAnnotationView *annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:AnnotationIdentifier] autorelease];
annotationView.canShowCallout = YES;
annotationView.image = [UIImage imageNamed:@"someImage.png"];
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:@selector(writeSomething:) forControlEvents:UIControlEventTouchUpInside];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
annotationView.rightCalloutAccessoryView = rightButton;
annotationView.canShowCallout = YES;
annotationView.draggable = YES;
return annotationView;
}
return nil;
}
#pragma标记-
#pragma标记MKMapView委托
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释
{
if([annotation isKindOfClass:[MKUserLocation类]])
返回零;
静态NSString*AnnotationIdentifier=@“AnnotationIdentifier”;
MKAnnotationView*annotationView=[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
如果(注释视图)
返回注释视图;
其他的
{
MKAnnotationView*annotationView=[[MKAnnotationView alloc]initWithAnnotation:annotation
reuseIdentifier:AnnotationIdentifier]autorelease];
annotationView.canShowCallout=是;
annotationView.image=[UIImage ImageName:@“someImage.png”];
UIButton*rightButton=[UIButton Button类型:UIButtonTypedTailButton];
[rightButton addTarget:self action:@selector(WriteMething:)for ControlEvents:UIControlEventTouchUpInside];
[rightButton setTitle:annotation.title for状态:UIControlStateNormal];
annotationView.rightCalloutAccessoryView=rightButton;
annotationView.canShowCallout=是;
annotationView.draggable=是;
返回注释视图;
}
返回零;
}
有关详细信息,请参见当用户位置首次更新时,您可能正在放置pin。正如您所知,位置可以在获得用户最准确的位置之前来回移动。您应该将pin放置在didUpdateLocations中,以便pin始终放置在最准确和最新的用户位置。