2017-10-06 116 views
0

新的編碼器嘗試在我看來適合GoogleMap。Swift GoogleMaps fitBounds Zoom

我搜查了很多信息,我得出了這個結論,但它不適用於我。

倍率FUNC的loadView(){

var markerList = [GMSMarker]() 

    // Create a GMSCameraPosition 
    let camera = GMSCameraPosition.camera(withLatitude: 4.390205, longitude: 2.154007, zoom: 8) 
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera) 
    mapView.isMyLocationEnabled = true 
    view = mapView 

    mapView.settings.myLocationButton = true 
    //mapView.setMinZoom(10, maxZoom: 20) 

    //create markers 
    for loc in arrayOfMapStops { 
     let marker = GMSMarker() 
     marker.position = CLLocationCoordinate2D(latitude: loc.lat, longitude: loc.long) 
     marker.title = loc.address 
     marker.snippet = loc.type 
     if loc.type == "Entrega" {marker.icon = GMSMarker.markerImage(with: .green)} 
     else {marker.icon = GMSMarker.markerImage(with: .blue)} 
     marker.map = mapView 
     markerList.append(marker) 
    } 

    //fit map to markers 
    var bounds = GMSCoordinateBounds() 
    for marker in markerList { 
     bounds = bounds.includingCoordinate(marker.position) 
    } 
    let update = GMSCameraUpdate.fit(bounds) 
    mapView.moveCamera(update) 
} 

的地圖不與適當的變焦調節。

image of situation

任何人都可以幫助我的縮放問題?

在此先感謝:)

+0

感謝建議@Nrzonline! –

回答

0

我自己解決了這個問題。我使用DispatchQueue將正確的縮放比例設置爲我的地圖。

這是我的最終代碼:

override func loadView() { 

    var markerList = [GMSMarker]() 

    // Create a GMSCameraPosition 
    let camera = GMSCameraPosition.camera(withLatitude: 40.4167 , longitude: -3.70325, zoom: 8) 
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera) 
    mapView.isMyLocationEnabled = true 
    view = mapView 

    mapView.settings.myLocationButton = true 
    //mapView.setMinZoom(10, maxZoom: 20) 

    //create markers 
    for loc in arrayOfMapStops { 
     let marker = GMSMarker() 
     marker.position = CLLocationCoordinate2D(latitude: loc.lat, longitude: loc.long) 
     marker.title = loc.address 
     marker.snippet = loc.type 
     if loc.type == "Entrega" {marker.icon = GMSMarker.markerImage(with: .green)} 
     else {marker.icon = GMSMarker.markerImage(with: .blue)} 
     marker.map = mapView 
     markerList.append(marker) 
    } 

    delay(seconds: 3) {() ->() in 
     //fit map to markers 
     var bounds = GMSCoordinateBounds() 
     for marker in markerList { 
      bounds = bounds.includingCoordinate(marker.position) 
     } 
     let update = GMSCameraUpdate.fit(bounds, withPadding: 100.0) 
     mapView.animate(with: update) 
    } 
} 

func delay(seconds: Double, completion:@escaping()->()) { 
    let when = DispatchTime.now() + seconds 
    DispatchQueue.main.asyncAfter(deadline: when) { 
     completion() 
    } 
} 

:)