2017-08-17 239 views
1

我有一個地圖上有一個自定義的UIView。我正在根據縮放級別計算圓的半徑。谷歌地圖半徑縮放級別

func getCenterCoordinate() -> CLLocationCoordinate2D { 
    let centerPoint = self.mapView.center 
    let centerCoordinate = self.mapView.projection.coordinate(for: centerPoint) 
    return centerCoordinate 
} 

func getTopCenterCoordinate() -> CLLocationCoordinate2D { 
    // to get coordinate from CGPoint of your map 
    let topCenterCoor = self.mapView.convert(CGPoint(x:self.circleView.frame.size.width/2.0, y:0), from: self.circleView) 
    let point = self.mapView.projection.coordinate(for: topCenterCoor) 
    return point 
} 

func getRadius() -> CLLocationDistance { 

    let centerCoordinate = getCenterCoordinate() 
    // init center location from center coordinate 
    let centerLocation = CLLocation(latitude: centerCoordinate.latitude, longitude: centerCoordinate.longitude) 
    let topCenterCoordinate = self.getTopCenterCoordinate() 
    let topCenterLocation = CLLocation(latitude: topCenterCoordinate.latitude, longitude: topCenterCoordinate.longitude) 

    let radius = CLLocationDistance(centerLocation.distance(from: topCenterLocation))/1000 
    print(radius) 
    return round(radius) 
} 

現在,我想根據給定的半徑縮放地圖?我怎麼能這樣做?

Image1

+0

你的地圖是長方形,那麼你會怎麼要映射的圓形到矩形?一旦你決定,你只需要調用'mapView.setRegion()'。 –

+0

請檢查我的答案。希望它能幫助你。 – Rex

回答

0

接在圓的周長的任何座標,並設置地圖的縮放或相機的變焦到的該座標的邊界,加入一些填充ofcourse。現在,對於這種邏輯,如果您的地圖是用戶啓用的縮放,每次您的地圖的相機移動或重新中心時,您都必須重新計算。

您可以使用此方法,可以這樣做:mapView.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 50.0))

這裏界定是一個GMSCoordinateBounds對象。

希望這有助於

+0

謝謝你的回答,但那不是我正在尋找的。例如,我在變焦級別4的半徑923處感謝上面發佈的那些函數。我想要做的是找到我需要的半徑爲10的縮放比例。 –

+0

這個答案會根據你的半徑自動適合你的縮放比例,你不必明確給出縮放級別 –

1

您可以在您創建一個擴展類: -

extension GMSCircle { 

    func bounds() -> GMSCoordinateBounds { 

     func locationMinMax(positive : Bool) -> CLLocationCoordinate2D { 

      let sign:Double = positive ? 1 : -1 
      let dx = sign * self.radius/6378000 * (180/Double.pi) 
      let lat = position.latitude + dx 
      let lon = position.longitude + dx/cos(position.latitude * .pi/180) 

      return CLLocationCoordinate2D(latitude: lat, longitude: lon) 
     } 

     return GMSCoordinateBounds(coordinate: locationMinMax(positive: true), 
            coordinate: locationMinMax(positive: false)) 
    } 

} 

局部變量

var cirlce: GMSCircle! 
var circleCenter = CLLocationCoordinate2D() 

現在你可以使用這個綁定到你的圈子變焦。

@IBAction func circleZoom(_ sender: Any) { 

    // Now Map Zoom with Circle size. 
    let update = GMSCameraUpdate.fit(cirlce.bounds()) 
    self.googleMaps.animate(with: update) 
} 

func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? { 

    let customInfoWindow = Bundle.main.loadNibNamed("Subview", 
                owner: self, 
                options: nil)?[0] as! CustomInfoWindow 

    customInfoWindow.title.text = marker.title! 
    customInfoWindow.button.tag = 0 
    customInfoWindow.button.addTarget(self, 
             action: #selector(YourViewController.buttonpush(sender:)), 
             for: .touchUpInside) 

    return customInfoWindow 
} 

// Location Manager 
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

    let location = locations.last 
    let camera = GMSCameraPosition.camera(withLatitude: (location?.coordinate.latitude)!, 
              longitude: (location?.coordinate.longitude)!, 
              zoom: zoom) 

    self.googleMaps?.animate(to: camera) 
    self.locationManager.stopUpdatingLocation() 
} 

當然,它會幫助你。我在Map上使用Slider。 示例屏幕截圖。第一次放大和第二次縮小。

enter image description here enter image description here

Here你可以找到更多的詳細信息關於圈的創建和地圖放大斯威夫特和目標C

+0

謝謝!但我的圈子是地圖頂部的UIView,而不是GMSCircle。 –

+0

@AntoineGarcia你想縮放地圖平行於圓形縮放嗎? – Rex

+0

該圓形保持在相同的半徑和地圖中心。我只想放大我的地圖,使圓圈內的區域爲X公里。 –

相關問題