2017-05-01 3 views
0

ルートマップの中央に画像を表示したいとします。この問題を解決するためにどのようにGoogleマップを使用してルートマップの中心を取得する方法

 let camera1 = GMSCameraPosition.camera(withLatitude: 45.4654, longitude:9.1859, zoom: 0.0) 
     self.mapView = GMSMapView.map(withFrame: cell.mapView.bounds, camera: camera1) 

Alamofire.request(url, method: .post, parameters: nil, encoding: JSONEncoding.default, headers: nil) 
     .validate() 
     .responseJSON { response in 
      switch response.result { 
      case .success: 
       print(response.result.value!) 
       print("Validation Successful") 
       let dictResponse = response.result.value as! NSDictionary 
       print(dictResponse) 
       let aryRoutes = dictResponse .value(forKey:"routes") as! NSArray 
       print(aryRoutes) 

       var aryOverViewPolyLines :NSArray = [] 
       aryOverViewPolyLines = aryRoutes .value(forKey: "overview_polyline") as! NSArray 
       print(aryOverViewPolyLines) 
       let strPoints = (aryOverViewPolyLines.value(forKey: "points") as! NSArray).object(at: 0) 
       let polygon = GMSPolygon() 
       polygon.path = GMSPath(fromEncodedPath: strPoints as! String) 
       print(strPoints) 
       let rectangle = GMSPolyline.init(path: polygon.path) 
       rectangle.strokeWidth = 2.0 
       rectangle.strokeColor = .white 
       rectangle.map = self.mapView 

       let mapBounds = GMSCoordinateBounds(path: polygon.path!) 
       self.mapView.animate(with: GMSCameraUpdate.fit(mapBounds, withPadding: 150.0)) 



       case .failure(let error): 
       print(response.result.value!) 
       print(error) 
      } 
    } 

任意の提案:以下 は、出発地と目的地との間方向に使用されるコードです。

答えて

0

これを試してください。

//        /** Degrees to Radian **/ 

class func degreeToRadian(angle:CLLocationDegrees) -> CGFloat{ 

    return (  (CGFloat(angle))/180.0 * CGFloat(M_PI) ) 

} 

//        /** Radians to Degrees **/ 

class func radianToDegree(radian:CGFloat) -> CLLocationDegrees{ 
    return CLLocationDegrees(  radian * CGFloat(180.0/M_PI) ) 
} 

class func middlePointOfListMarkers(listCoords: [CLLocationCoordinate2D]) -> CLLocationCoordinate2D{ 

    var x = 0.0 as CGFloat 

    var y = 0.0 as CGFloat 

    var z = 0.0 as CGFloat 

    for coordinate in listCoords{ 

         var lat:CGFloat = degreeToRadian(coordinate.latitude) 

        var lon:CGFloat = degreeToRadian(coordinate.longitude) 

        x = x + cos(lat) * cos(lon) 

        y = y + cos(lat) * sin(lon); 

        z = z + sin(lat); 

    } 

    x = x/CGFloat(listCoords.count) 

    y = y/CGFloat(listCoords.count) 

    z = z/CGFloat(listCoords.count) 

  var resultLon: CGFloat = atan2(y, x) 

    var resultHyp: CGFloat = sqrt(x*x+y*y) 

    var resultLat:CGFloat = atan2(z, resultHyp) 

    var newLat = radianToDegree(resultLat) 

    var newLon = radianToDegree(resultLon) 

    var result:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: newLat, longitude: newLon) 

    return result 
} 
0
func mapView(_ mapView: GMSMapView, didTap overlay: GMSOverlay) { 
    if let route = overlay as? GMSPolyline{ 

     let coordinate = route.path!.coordinate(at: (route.path!.count()-1)/2) 
     route.customMapInfoMarker!.position = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude) 
     route.customMapInfoMarker.groundAnchor = CGPoint(x: 0.5, y: 0.5) 
     route.customMapInfoMarker.map = mapView 
     route.customMapInfoMarker.iconView = // YOUR CUSTOM IMAGEVIEW HERE 
    } 
} 

extension GMSPolyline{ 

    fileprivate struct AssociatedKey { 
     static var infoMarker = GMSMarker() 
    } 
    var customMapInfoMarker: GMSMarker! { 
     get { 
      if let marker = objc_getAssociatedObject(self, &AssociatedKey.infoMarker) as? GMSMarker { 
       return marker 
      } 
      return GMSMarker() 
     } 
     set (newValue){ 
      if let newValue = newValue { 
       objc_setAssociatedObject(
        self, 
        &AssociatedKey.infoMarker, 
        newValue, 
        objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC) 
      } 
     } 
    } 
} 
GMSPolylineを拡張
関連する問題