gpt4 book ai didi

iphone - MapKit 不显示当前位置的蓝点

转载 作者:行者123 更新时间:2023-12-03 20:21:19 27 4
gpt4 key购买 nike

我是 iPhone 版 MapKit 的新手,尝试实现此功能,由于某种原因我看不到当前位置蓝点,其他人也有这个问题吗???

#import "DetailMapViewController.h"
#import "mapAnnotations.h"

@implementation DetailMapViewController

@synthesize inStock;

-(void)getlocation:(CLLocationCoordinate2D)loc
{
location = loc;
}

- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = @"Street View";
mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
mapView.delegate=self;
//MKCoordinateRegion region;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, 5000, 5000);

mapAnnotations *ann = [[mapAnnotations alloc] init];
ann.title = @"";
ann.subtitle = @"";
ann.coordinate = region.center;

mapView.showsUserLocation = YES;
[mapView addAnnotation:ann];
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
[self.view insertSubview:mapView atIndex:0];
}


- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];

if (annotation == mapView.userLocation)
{

annView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"blueDot"];
if (annView != nil)
{
annView.annotation = annotation;
}
else
{
annView = [[[NSClassFromString(@"MKUserLocationView") alloc] initWithAnnotation:annotation reuseIdentifier:@"blueDot"] autorelease];


}
}

if([inStock isEqual:@"yes"]){
annView.pinColor = MKPinAnnotationColorGreen;
}
if([inStock isEqual:@"no"]){
annView.pinColor = MKPinAnnotationColorRed;
}
if([inStock isEqual:@"unknown"]){

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"greyPin.png"]];
[annView addSubview:imageView];




}
annView.animatesDrop=TRUE;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
return annView;
}

- (void)dealloc {
[super dealloc];
}


@end

最佳答案

按照 viewForAnnotation 当前的编写方式,当尝试显示当前位置时,它实际上应该崩溃,因为“蓝点”注释 View 没有 pinColor 或 animatesDrop 属性。

尝试将其更改为以下内容:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
if ([annotation isKindOfClass:MKUserLocation.class]) {
//user location view is being requested,
//return nil so it uses the default which is a blue dot...
return nil;
}

MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];

if([inStock isEqual:@"yes"]){
annView.pinColor = MKPinAnnotationColorGreen;
}
if([inStock isEqual:@"no"]){
annView.pinColor = MKPinAnnotationColorRed;
}
if([inStock isEqual:@"unknown"]){
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"greyPin.png"]];
[annView addSubview:imageView];
}
annView.animatesDrop=TRUE;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
return annView;
}

在模拟器上,用户的位置将为美国加利福尼亚州库比蒂诺(旧金山以南一点)。如果您自己的注释不在 5000 米范围内,您将看不到蓝点。您必须缩小才能看到它。

关于iphone - MapKit 不显示当前位置的蓝点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4055973/

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