2016-02-03 3 views
3

Я пишу свое первое приложение для iOS. Я пытаюсь загрузить слой GeoJSON на карту google в приложении (карта происходит из карт Google sdk), но я не могу найти способ сделать это. Я владею API-интерфейсом API Google Maps, но я чувствую, что вещи в Swift очень разные.Добавление слоя geojson в google map в iOS

Как загрузить слой GeoJSON на карту в родном приложении для iOS?

+0

Вы можете видеть [Обеспечение проезда] (https://developer.apple.com/library /ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/ProvidingDirections/ProvidingDirections.html#//apple_ref/doc/uid/TP40009497-CH8-SW1). Я думаю, что это будет хорошее место, чтобы начать использовать GeoJSON в iOS. – gerardnimo

+0

У вас есть файл geojson? – Robert

+0

Вы нашли решение? –

ответ

1

На сегодняшний день я не вижу, чтобы какой-либо api мог разобрать geojson в фигуры google map на ios. Для этого вам нужно разобрать его самостоятельно, проанализировать его в массиве, затем выполнить цикл через массив, получить каждую функцию, получить каждую геометрию, свойства и создать форму на основе типа объекта (точка, линия, многоугольник)

Здесь образец из mapbox, чтобы нарисовать линию, вы должны расширить ее до точки и полигона.

// Perform GeoJSON parsing on a background thread 
dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
dispatch_async(backgroundQueue, ^(void) 
{ 
    // Get the path for example.geojson in the app's bundle 
    NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"example" ofType:@"geojson"]; 

    // Load and serialize the GeoJSON into a dictionary filled with properly-typed objects 
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:[[NSData alloc] initWithContentsOfFile:jsonPath] options:0 error:nil]; 

    // Load the `features` dictionary for iteration 
    for (NSDictionary *feature in jsonDict[@"features"]) 
    { 
     // Our GeoJSON only has one feature: a line string 
     if ([feature[@"geometry"][@"type"] isEqualToString:@"LineString"]) 
     { 
      // Get the raw array of coordinates for our line 
      NSArray *rawCoordinates = feature[@"geometry"][@"coordinates"]; 
      NSUInteger coordinatesCount = rawCoordinates.count; 

      // Create a coordinates array, sized to fit all of the coordinates in the line. 
      // This array will hold the properly formatted coordinates for our MGLPolyline. 
      CLLocationCoordinate2D coordinates[coordinatesCount]; 

      // Iterate over `rawCoordinates` once for each coordinate on the line 
      for (NSUInteger index = 0; index < coordinatesCount; index++) 
      { 
       // Get the individual coordinate for this index 
       NSArray *point = [rawCoordinates objectAtIndex:index]; 

       // GeoJSON is "longitude, latitude" order, but we need the opposite 
       CLLocationDegrees lat = [[point objectAtIndex:1] doubleValue]; 
       CLLocationDegrees lng = [[point objectAtIndex:0] doubleValue]; 
       CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(lat, lng); 

       // Add this formatted coordinate to the final coordinates array at the same index 
       coordinates[index] = coordinate; 
      } 

      // Create our polyline with the formatted coordinates array 
      MGLPolyline *polyline = [MGLPolyline polylineWithCoordinates:coordinates count:coordinatesCount]; 

      // Optionally set the title of the polyline, which can be used for: 
      // - Callout view 
      // - Object identification 
      // In this case, set it to the name included in the GeoJSON 
      polyline.title = feature[@"properties"][@"name"]; // "Crema to Council Crest" 

      // Add the polyline to the map, back on the main thread 
      // Use weak reference to self to prevent retain cycle 
      __weak typeof(self) weakSelf = self; 
      dispatch_async(dispatch_get_main_queue(), ^(void) 
      { 
       [weakSelf.mapView addAnnotation:polyline]; 
      }); 
     } 
    } 

}); 

здесь скор код строки, вы должны затратить его точку и многоугольник

// Parsing GeoJSON can be CPU intensive, do it on a background thread 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { 
     // Get the path for example.geojson in the app's bundle 
     let jsonPath = NSBundle.mainBundle().pathForResource("example", ofType: "geojson") 
     let jsonData = NSData(contentsOfFile: jsonPath!) 

     do { 
      // Load and serialize the GeoJSON into a dictionary filled with properly-typed objects 
      if let jsonDict = try NSJSONSerialization.JSONObjectWithData(jsonData!, options: []) as? NSDictionary { 

       // Load the `features` array for iteration 
       if let features = jsonDict["features"] as? NSArray { 
        for feature in features { 
         if let feature = feature as? NSDictionary { 
          if let geometry = feature["geometry"] as? NSDictionary { 
           if geometry["type"] as? String == "LineString" { 
            // Create an array to hold the formatted coordinates for our line 
            var coordinates: [CLLocationCoordinate2D] = [] 

            if let locations = geometry["coordinates"] as? NSArray { 
             // Iterate over line coordinates, stored in GeoJSON as many lng, lat arrays 
             for location in locations { 
              // Make a CLLocationCoordinate2D with the lat, lng 
              let coordinate = CLLocationCoordinate2DMake(location[1].doubleValue, location[0].doubleValue) 

              // Add coordinate to coordinates array 
              coordinates.append(coordinate) 
             } 
            } 

            let line = MGLPolyline(coordinates: &coordinates, count: UInt(coordinates.count)) 

            // Optionally set the title of the polyline, which can be used for: 
            // - Callout view 
            // - Object identification 
            line.title = "Crema to Council Crest" 

            // Add the annotation on the main thread 
            dispatch_async(dispatch_get_main_queue(), { 
             // Unowned reference to self to prevent retain cycle 
             [unowned self] in 
             self.mapView.addAnnotation(line) 
            }) 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
     catch 
     { 
      print("GeoJSON parsing failed") 
     } 
    }) 
+0

использует MapBox, а не карту Google api? – lee

+0

Я получаю сообщение об ошибке как завершающее приложение из-за неперехваченного исключения «NSInvalidArgumentException», причина: «Многоточечная точка должна иметь хотя бы одну вершину». внутренняя петля для местоположения в местах { –

+0

Можете ли вы посоветовать мне, как туда добраться? –

Смежные вопросы