iOS的:显示不同的引脚基于地图的JSON [英] iOS: show different pin based on json on map

查看:182
本文介绍了iOS的:显示不同的引脚基于地图的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON文件,并在此JSON我有不同的纬度和经度和状态。我想检查我的状态,并根据我的地图lat和长秀状态引脚。我的意思是,如果我的状态激活:绿针,如果我的状态已过期显示红色,如果我的身份是保密的秀脚灰

I have a json file and in this json I have different latitude and longitude and status. I want to check my status and based on lat and long show status pin on my map. I mean if my status is active : green pin if my status is expired show Red if my status is confidential show gray pin.

下面是我的JSON:

[
{
"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是我的方法:

I can print all json on my log here is my method :

-(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);
}

我有3个引脚形象我的X code我也有图形页面。我的问题是如何根据我在地图上的json显示这3个引脚?

I have 3 pin image in my xcode I also have mapView. My question is how to show this 3 pins based on my json on map?

在此先感谢!
鸭preciate如果你能为我提供code

Thanks in advance! Appreciate if you can provide me the code

推荐答案

我有此示范项目的为您服务。

为了显示自己的注释,你首先需要做,确认 MKAnnotation 协议的类。让我们把这个类位置。你需要这个类的一个实例添加到地图告诉你的地图标注的位置。您可以选择添加标题和微妙的这个类来告诉地图标注的标题和副标题。

In order to show your own annotation, you need first to make a class that confirms MKAnnotation protocol. Let's call this class Location. You need to add a instance of this class into the map to tell the map the position of your annotation. You can optionally add title and subtile into this class to tell the map the title and subtitle of the annotation.

@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

现在转到包含地图视图控制器。创建并添加注释到地图。此外,地图的委托设置为

Now go to the view controller that contains the map. Create and add an annotation into the map. Also, set the delegate of the map to self:

Location * myLocation = [[Location alloc]initWithCoordinate:CLLocationCoordinate2DMake(56.715866, -118.281757) title:@"My Land" subTitle:@"haha~"];
[_mapView addAnnotation:myLocation];
_mapView.delegate = self;

控制器应以显示自定义批注确认为 MKMapViewDelegate 。告诉地图视图如何显示注释的方法是:

The controller should confirm to MKMapViewDelegate in order to show custom annotation. The method that tell the map view how to display an annotation is:

- (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; ,这样,当你触摸注解,它会显示在你的标注的标题和微妙

You can set view.canShowCallout = YES;, so that when you touch the annotation, it will show the title and subtile in your annotation.

如果你有一个以上的注解,它可以方便地添加属性 @属性(非原子,强)的UIImage *图片位置类。能容纳注释的形象,并在图形页面:viewForAnnotation:设置 view.image = annotation.image

If you have more than one annotations, it will be convenient to add a property @property (nonatomic, strong) UIImage * image into your Location class. Which can hold the image of the annotation, and in mapView:viewForAnnotation: set view.image = annotation.image.

这篇关于iOS的:显示不同的引脚基于地图的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆