gpt4 book ai didi

ios - 如何在 mapKit 框架中使用自定义图标?

转载 作者:可可西里 更新时间:2023-11-01 03:08:20 24 4
gpt4 key购买 nike

我正在使用 MapKit 框架在我的应用程序上加载谷歌地图,并在 map 上放置 4 个“模拟”位置,如下所示:

- (void)viewDidLoad {

[super viewDidLoad];

mapView.delegate = self;

mapView.showsUserLocation = YES;

MKUserLocation *userLocation = mapView.userLocation;

MKCoordinateRegion region =
MKCoordinateRegionMakeWithDistance (userLocation.location.coordinate,500,500);
[mapView setRegion:region animated:NO];

//Simulated annotations on the map
CLLocationCoordinate2D poi1Coord , poi2Coord , poi3Coord , poi4Coord;
//poi1 coordinates
poi1Coord.latitude = 37.78754;
poi1Coord.longitude = -122.40718;
//poi2 coordinates
poi2Coord.latitude = 37.78615;
poi2Coord.longitude = -122.41040;
//poi3 coordinates
poi3Coord.latitude = 37.78472;
poi3Coord.longitude = -122.40516;
//poi4 coordinates
poi4Coord.latitude = 37.78866;
poi4Coord.longitude = -122.40623;

MKPointAnnotation *poi1 = [[MKPointAnnotation alloc] init];
MKPointAnnotation *poi2 = [[MKPointAnnotation alloc] init];
MKPointAnnotation *poi3 = [[MKPointAnnotation alloc] init];
MKPointAnnotation *poi4 = [[MKPointAnnotation alloc] init];


poi1.coordinate = poi1Coord;
poi2.coordinate = poi2Coord;
poi3.coordinate = poi3Coord;
poi4.coordinate = poi4Coord;

poi1.title = @"McDonald's";
poi1.subtitle = @"Best burgers in town";
poi2.title = @"Apple store";
poi2.subtitle = @"Iphone on sales..";
poi3.title = @"Microsoft";
poi3.subtitle = @"Microsoft's headquarters";
poi4.title = @"Post office";
poi4.subtitle = @"You got mail!";

[mapView addAnnotation:poi1];
[mapView addAnnotation:poi2];
[mapView addAnnotation:poi3];
[mapView addAnnotation:poi4];

代码相当简单。我想做的是不要在我的谷歌地图上使用典型的红色图钉,而是使用我自己的图像来显示不同的地方。有没有一种简单/直接的方法可以做到这一点,因为我对已经找到的样本感到很困惑。

最佳答案

您可以使用以下代码更改注释图像,

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
if ([[annotation title] isEqualToString:@"Current Location"]) {
return nil;
}

MKAnnotationView *annView = [[MKAnnotationView alloc ] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
if ([[annotation title] isEqualToString:@"McDonald's"])
annView.image = [ UIImage imageNamed:@"mcdonalds.png" ];
else if ([[annotation title] isEqualToString:@"Apple store"])
annView.image = [ UIImage imageNamed:@"applestore.png" ];
else
annView.image = [ UIImage imageNamed:@"marker.png" ];
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[infoButton addTarget:self action:@selector(showDetailsView)
forControlEvents:UIControlEventTouchUpInside];
annView.rightCalloutAccessoryView = infoButton;
annView.canShowCallout = YES;
return annView;
}

其中“marker.png”是您的图像文件。

关于ios - 如何在 mapKit 框架中使用自定义图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10797689/

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