gpt4 book ai didi

ios - 绘制两种不同颜色的 map View 图钉

转载 作者:行者123 更新时间:2023-11-29 13:17:23 27 4
gpt4 key购买 nike

我正在尝试显示带有坐标数组的 map View 。所以,我从包含这些的数组中加载纬度和经度。它显示完美。现在,我想用不同的颜色图钉显示一个特定的纬度和经度。我提到了this answer我看不出有什么不同。

ViewController.m


-(void)showMap:
{
...
...
DataProvider *d = [DataProvider getInstance];
NSInteger numb = sender.view.tag;
d.colorlatitude = [[arraxy objectAtIndex:numb] objectForKey:@"lat"];
d.colorlongitude = [[arraxy objectAtIndex:numb] objectForKey:@"lng"];
[d._address removeAllObjects];
[arraxy removeObjectAtIndex:sender.view.tag];
d._address = arraxy;

MapViewController *map = [[MapViewController alloc]initWithNibName:@"MapViewController" bundle:nil];
[self.navigationController pushViewController:map animated:YES];

}

MapViewController.m


-(void)viewWillAppear:(BOOL)animated
{
DataProvider *d = [DataProvider getInstance];
[_mapView removeAnnotations:_mapView.annotations];

RegisterViewController *appDelegate = [[RegisterViewController alloc]init];

[_mapView setShowsUserLocation:YES];
[_mapView setRegion:MKCoordinateRegionMakeWithDistance([appDelegate.locationManager location].coordinate, 1000, 1000)];
[_mapView setUserTrackingMode:MKUserTrackingModeNone];

MKCoordinateRegion rregion = {{0.0,0.0},{0.0,0.0}};
rregion.center.latitude = [d.colorlatitude floatValue];
rregion.center.longitude = [d.colorlongitude floatValue];
rregion.span.latitudeDelta=0.001f;
rregion.span.longitudeDelta=0.001f;
[_mapView setRegion:rregion];

MapviewAnnotations *add = [[MapviewAnnotations alloc]init];
add.coordinate = rregion.center;
[_mapView addAnnotation:add];


if (d._address)
{
for (int i=0; i<[d._address count]; i++)
{
NSDictionary *dic=[d._address objectAtIndex:i];
MKCoordinateRegion region={{0.0,0.0},{0.0,0.0}};
region.center.latitude=[[dic objectForKey:@"lat"]floatValue];
region.center.longitude=[[dic objectForKey:@"lng"]floatValue];
region.span.latitudeDelta=0.001f;
region.span.longitudeDelta=0.001f;
[_mapView setRegion:region];

MapviewAnnotations *ann=[[MapviewAnnotations alloc]init];
ann.coordinate=region.center;
[_mapView addAnnotation:ann];
}
}
[super viewWillAppear:YES];
}

我的MKMapView的委托(delegate)方法是

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id  <MKAnnotation>)annotation
{
if (![annotation isKindOfClass:[MapviewAnnotations class]])
{
return nil;
}

static NSString *reuseId = @"currentloc";

MKPinAnnotationView *annView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (annView == nil)
{
annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
annView.animatesDrop = NO;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
}
else
{
annView.annotation = annotation;
}

DataProvider *mvAnn = [DataProvider getInstance];
if (mvAnn.colorlatitude) // here i'm checking the condition.
{
annView.pinColor = MKPinAnnotationColorGreen;
}
else
{
annView.pinColor = MKPinAnnotationColorRed;
}

return annView;
}

我只是在写条件,如果坐标在那里,我只需要将绿色引脚绘制到该特定坐标。如何实现?

最佳答案

您的代码与 Hinata 提到的代码之间的区别在于,其他代码中的 if 语句在当前正在绘制的注释上使用一个值 (yesno) 来决定要绘制什么颜色使用。您正在从 DataProvider 获取一个值,但没有告诉它您正在绘制哪个注释,因此它为您提供的实例正是 instance 方法当时返回的感觉 map 要求提供图钉。您需要告诉它您要绘制什么,以便它决定将什么放入 colorlatitude

关于ios - 绘制两种不同颜色的 map View 图钉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15382628/

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