2017-10-06 28 views
0

新しいコーダーはGoogleマップを自分の視点に合わせようとします。スウィフトGoogleマップfitBoundsズーム

私は多くの情報を検索しましたが、この結論に至りましたが、それは私のためには機能しません。

オーバーライド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

感謝:

は、ここに私の最終的なコードです! –

答えて

0

私は自分でこの問題を解決しました。 DispatchQueueを使用してマップに適切なズームを設定します。 @Nrzonlineを示唆するため

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() 
    } 
} 

:)

関連する問題