iOS:根据地图上的json显示不同的pin
我有一个json文件,在这个json中我有不同的纬度、经度和状态。我想检查我的状态,并基于我的地图上的lat和long show状态pin。我的意思是,如果我的状态是活动的:绿色pin如果我的状态已过期显示红色如果我的状态是机密显示灰色pin 这是我的json:iOS:根据地图上的json显示不同的pin,ios,json,annotations,mapkit,Ios,Json,Annotations,Mapkit,我有一个json文件,在这个json中我有不同的纬度、经度和状态。我想检查我的状态,并基于我的地图上的lat和long show状态pin。我的意思是,如果我的状态是活动的:绿色pin如果我的状态已过期显示红色如果我的状态是机密显示灰色pin 这是我的json: [ { "status": "active", "latitude": 56.715866, "longitude": -118.281757 }, { "status": "expired", "latitude": 56.71586
[
{
"status": "active",
"latitude": 56.715866,
"longitude": -118.281757
},
{
"status": "expired",
"latitude": 56.715860,
"longitude": -118.281757
},
{
"status": "confidential",
"latitude": 56.715111,
"longitude": -118.281117
}]
我可以在我的日志上打印所有json这里是我的方法:
-(void)getPin
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:BASED_URL]];
[request setHTTPMethod:@"GET"];
[request setValue:@"/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
NSURLResponse *response;
NSData *GETReply = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response error:nil];
NSString *theReply = [[NSString alloc] initWithBytes:[GETReply bytes] length:[GETReply
length] encoding: NSASCIIStringEncoding];
NSLog(@"Reply: %@", theReply);
}
我的xcode中有3针图像,我还有mapView。我的问题是如何基于我的json在地图上显示这3个管脚
提前谢谢!
如果您能提供我为您制作的代码,我将不胜感激
为了显示您自己的注释,您首先需要创建一个确认MKAnnotation协议的类。让我们称这个类为Location。您需要将此类的实例添加到地图中,以告知地图注释的位置。您可以选择将title和subtile添加到此类中,以告知地图注释的标题和副标题
@interface Location : NSObject<MKAnnotation>
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, readonly, copy) NSString *title;
@property (nonatomic, readonly, copy) NSString *subtitle;
- (instancetype)initWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title subTitle:(NSString *)subTitle;
@end
控制器应确认MKMapViewDelegate以显示自定义注释。告诉地图视图如何显示注释的方法是:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation
{
MKAnnotationView * view = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"Location"];
view.image = [UIImage imageNamed:@"someImage"];
view.canShowCallout = YES;
return view;
}
您可以设置view.canShowCallout=YES;,因此,当您触摸注释时,它将显示注释中的标题和子文件
@interface Location : NSObject<MKAnnotation>
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, readonly, copy) NSString *title;
@property (nonatomic, readonly, copy) NSString *subtitle;
- (instancetype)initWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title subTitle:(NSString *)subTitle;
@end
如果您有多个注释,则可以方便地将property@property非原子、强UIImage*图像添加到Location类中。它可以保存注释的图像,在mapView:viewForAnnotation:set view.image=annotation.image中。您知道如何解析Jason文件吗?如果你知道,你可以继续读我的答案。我的答案是关于如何添加注释。从JSON添加注释的示例代码:。对于pin颜色,将status属性添加到自定义注释类中,如@Sen所示,并实现viewForAnnotation委托方法,如下所示:。感谢您的评论,您是否阅读了我的问题,我说过我可以阅读json,而且我的问题是如何从json@SaraJurdan您可以将Json数据转换为一个位置数组。并使用上面提到的方法将它们添加到地图中。