如何在线添加多个引脚/注释?

时间:2013-08-20 11:43:27

标签: ios mkmapview mkpointannotation

我正在尝试从服务器添加引脚并将其显示在地图上。

通常我可以显示引脚但我想在线添加它。我有三个解析的json数据NAME,Longitude和Latitude。我在数组中解析了它。我不知道如何在地图上查看它

CLLocationCoordinate2D annotationCoord;

annotationCoord.latitude = 40.0000;
annotationCoord.longitude = 29.000;

MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = annotationCoord;
annotationPoint.title = name;

[self.locationMap addAnnotation:annotationPoint]; 

我尝试在annotationCoord.latitude循环中添加annotationCoord.longitudefor,但我收到此错误“错误的接收器类型'CLLocationDegrees'(又名double)”我想我我犯了大错,但我无法知道。请帮忙。

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{

for (int i = 0; i < jsonArray.count; i++) {

    NSString *lat = [jsonArray objectAtIndex:i];

    [annotationCoord.latitude addObject:lat];

}

}

我的JSON回归:

 response = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://kalkatawi.com/mapLocation.php"]];

 NSError *parseError = nil;

    jsonArray = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingAllowFragments error:&parseError];

    jsonArray1 = [[NSMutableArray alloc] init];
    jsonArray2 = [[NSMutableArray alloc] init];
    jsonArray3 = [[NSMutableArray alloc] init];

    for(int i=0;i<[jsonArray count];i++)
    {
        name = [[jsonArray objectAtIndex:i] objectForKey:@"name"];

        [jsonArray1 addObject:name];            
    }

    for(int i=0;i<[jsonArray count];i++)
    {
        longitude = [[jsonArray objectAtIndex:i] objectForKey:@"longitude"];

        [jsonArray2 addObject:longitude];            
    }

    for(int i=0;i<[jsonArray count];i++)
    {
        latitude = [[jsonArray objectAtIndex:i] objectForKey:@"latitude"];

        [jsonArray3 addObject:latitude];            
    }

    self.locationMap.delegate = self;

3 个答案:

答案 0 :(得分:0)

尝试

- (void)addAnnotations:(NSArray *)annotations;
MKMApView的

答案 1 :(得分:0)

根据更新后的代码,jsonArray已经是一个字典数组,每个字典都包含每个注释的属性。

我不明白你为什么要将它分成三个独立的数组(每个属性一个)。

为什么不使用jsonArray来创建注释:

jsonArray = [NSJSONSerialization JSONObjectWithData:...

self.locationMap.delegate = self;  //set delegate before adding annotations

for (int i=0; i < [jsonArray count]; i++)
{
    NSDictionary *annotationDictionary = [jsonArray objectAtIndex:i];

    name = [annotationDictionary objectForKey:@"name"];

    annotationCoord.latitude 
      = [[annotationDictionary objectForKey:@"latitude"] doubleValue];
    annotationCoord.longitude 
      = [[annotationDictionary objectForKey:@"longitude"] doubleValue];

    MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
    annotationPoint.coordinate = annotationCoord;
    annotationPoint.title = name;

    [self.locationMap addAnnotation:annotationPoint]; 
}

问题中的更新代码还显示它在jsonArray委托方法中循环didUpdateUserLocation。目前尚不清楚为什么要在该委托方法中完成此操作,但如果您计划在每次用户移动时更新地图上的注释,您可能还需要在再次添加之前删除所有/一些现有注释以避免重复注释。

答案 2 :(得分:0)

顶部答案给出的解释。我只将此代码转换为Swift 3。

Swift 3

jsonArray = JSONSerialization.jsonObjectWithData()
locationMap.delegate = self

for i in 0..<jsonArray.count() {
var annotationDictionary = jsonArray[i]
name = annotationDictionary["name"]
annotationCoord.latitude = annotationDictionary["latitude"]!
annotationCoord.longitude = annotationDictionary["longitude"]!
var annotationPoint = MKPointAnnotation()
annotationPoint.coordinate = annotationCoord
annotationPoint.title = name
locationMap.addAnnotation(annotationPoint)
}