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

问题描述 投票:0回答:3

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

通常我可以显示引脚,但我想在线添加它。我有三个解析的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'(akadouble)”我认为我犯了大错,但我无法知道。请帮忙。

- (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;
ios mkmapview mkpointannotation
3个回答
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。目前尚不清楚为什么要在该委托方法中完成此操作,但如果您计划在每次用户移动时更新地图上的注释,您可能还需要在再次添加之前删除所有/一些现有注释以避免重复注释。


0
投票

尝试

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

MKMApView


0
投票

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

斯威夫特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)
}
© www.soinside.com 2019 - 2024. All rights reserved.