2017-05-18 3 views
0

Ich arbeite derzeit mit dem iOS Google Map SDK, und ich möchte einige Polygone auf meiner Karte zu zeichnen.iOS - So erstellen Sie ein Wörterbuch von GMSPolygons mit ihrer GMSMutablePath aus JSON-Datei

Genauer gesagt, ich habe ein JSON mit verschiedenen Lat/Long-Punkte für eine bestimmte Abteilung.

Ich bin in der Lage, eine GMSPolygon durch Erstellen einer GMSMutablePath mit nur wenigen Punkten, wie in der Google-Dokumentation dargestellt zu zeichnen.

Was ich am anderen Ende nicht erreichen kann, ist, es generisch zu machen.

Lassen Sie mich Ihnen etwas Code zeigen.

{"01": [[51.01, 2.07], [50.99, 2.12], [51.01, 2.11], [51.03, 2.15], ...], "02": [[50.51, 1.64], [50.51, 1.66], ...]} 

Ich habe diese JSON-Datei, die Tausende von Positionspunkten für eine bestimmte Abteilung enthält. Ich habe es gekürzt, so dass Sie nur das Format des JSON-Objekts sehen können.

Hier ist meine Analyse:

-(void) parseJsonDept { 
    NSString *jsonFilePath = [[NSBundle mainBundle] pathForResource:@"dept" ofType:@"json"]; 
    if (!jsonFilePath) { 
     NSLog(@"Not a valid FilePath"); 
     return; 
    } 

    NSString *myJSON = [[NSString alloc] initWithContentsOfFile:jsonFilePath encoding:NSUTF8StringEncoding error:NULL]; 
    if (!myJSON) { 
     NSLog(@"File couldn't be read!"); 
     return; 
    } 

    NSError *error = nil; 
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[myJSON dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error]; 
} 

Sehr einfach, und jetzt weiß ich nicht, wie sie damit umgehen.

Ich habe dieses Stück Code aus der Google-Dokumentation, um ein Polygon zu zeichnen.

-(void) drawPolygon { 
    // Create a rectangular path 
    GMSMutablePath *rect = [GMSMutablePath path]; 
    [rect addCoordinate:CLLocationCoordinate2DMake(51.01, 2.07)]; 
    [rect addCoordinate:CLLocationCoordinate2DMake(50.99, 2.12)]; 
    [rect addCoordinate:CLLocationCoordinate2DMake(51.03, 2.15)]; 
    [rect addCoordinate:CLLocationCoordinate2DMake(51.01, 2.18)]; 

    // Create the polygon, and assign it to the map. 
    GMSPolygon *polygon = [GMSPolygon polygonWithPath:rect]; 
    polygon.fillColor = [UIColor colorWithRed:0.25 green:0 blue:0 alpha:0.05]; 
    polygon.strokeColor = [UIColor blackColor]; 
    polygon.strokeWidth = 2; 
    polygon.map = _mapView; 
} 

Meine Frage ist, wie ein Wörterbuch von GMSPolygons ihrer GMSMutablePath und ihre Department verbundenen erstellen.

Zum Beispiel Schleifen jeder Abteilung aus meinem JSON-Wörterbuch, und [rect addCoordinate:CLLocationCoordinate2DMake(lat, long)] Hinzufügen von Koordinaten zu meinem GMSMutablePath.

Und so weiter für jede Abteilung, so dass ich am Ende habe ich meine Liste von Polygonen.

Ich habe mehr mit JSON zu kämpfen, als ich es erwartet ...

Antwort

1

Nachdem Sie bekommen Dictionary Sie Schleife durch sie kann und GMSMutablePath daraus machen.

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[myJSON dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error]; 
GMSMutablePath *rect = [GMSMutablePath path]; 
for(id key in json) { 
    NSArray *coordinates = [json objectForKey:key]; 
    for(NSArray *coordinate in coordinates) { 
     double latitude = [[coordinate firstObject] doubleValue]; 
     double longitude = [[coordinate lastObject] doubleValue]; 
     [rect addCoordinate:CLLocationCoordinate2DMake(latitude, longitude)]; 
    } 
} 
// Create the polygon, and assign it to the map. 
GMSPolygon *polygon = [GMSPolygon polygonWithPath:rect]; 
polygon.fillColor = [UIColor colorWithRed:0.25 green:0 blue:0 alpha:0.05]; 
polygon.strokeColor = [UIColor blackColor]; 
polygon.strokeWidth = 2; 
polygon.map = _mapView; 
+0

@RaphaelP. Willkommen Kumpel :) –

+0

@RaphaelP. Ersetzen Sie einfach "Array" zu "Koordinate" Tippfehler :) Ich habe die Antwort auch bearbeitet –

Verwandte Themen