0

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

通常我可以显示图钉,但我想在线添加它。我有三个解析的 json 数据名称、经度和纬度。我已经在数组中解析了它。我不知道如何在地图上查看它

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和,但我得到这个错误“错误的接收器类型'CLLocationDegrees'(又名)”我想我犯了很大的错误,但我不知道。请需要帮助。annotationCoord.longitudefordouble

- (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;
4

3 回答 3

0

尝试

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

MKMApView 的

于 2013-08-20T12:14:01.640 回答
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委托方法中循环。目前尚不清楚为什么要在该委托方法中执行此操作,但如果您计划在每次用户移动时更新地图上的注释,您可能还需要在再次添加之前删除所有/一些现有注释以避免重复注释。

于 2013-08-21T02:25:59.570 回答
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)
}
于 2017-12-23T13:18:42.330 回答