2016-09-01 40 views
0

相比,Google地圖在允許顯示在主視圖上時效果很好。谷歌地圖顯示不同的位置,與輸入

但是,當我使用新的Sub時,地圖的位置是錯誤的,即使經緯度值發生變化也不會改變。

代碼段:

import UIKit 
import GoogleMaps 

class MainVC: UIViewController { 


@IBOutlet weak var mapSubView: GMSMapView! 

override func viewDidLoad() { 

    let camera = GMSCameraPosition.cameraWithLatitude(13.009047, longitude: 77.652949, zoom: 6) 
    let mapView = GMSMapView.mapWithFrame(self.mapSubView.bounds, camera: camera) 

    mapView.myLocationEnabled = true 

    self.mapSubView = mapView 


    // Creates a marker in the center of the map. 
    let marker = GMSMarker() 
    marker.position = CLLocationCoordinate2D(latitude: 13.009047, longitude: 77.652949) 
    marker.title = "Bangalore" 
    marker.snippet = "India" 
    marker.map = mapView 
} 

} 

應用截圖: enter image description here

+0

嘗試放大並檢查是否在正確的位置 – Dravidian

+0

號的創建您的標誌標記不可見 –

回答

1

此行添加到您的代碼: -

mapSubView.camera = GMSCameraPosition(target: CLLocationCoordinate2D(latitude: 13.009047, longitude: 77.652949), zoom: 15, bearing: 0, viewingAngle: 0) 

GoogleMaps相機只是定義地圖的方位與沿屬性,如你想要多少縮放,什麼是縮放等目標。關於這個功能的更多細節d什麼參數表示CMD +點擊此功能可以找到它的文件建立

編輯: 上面的代碼中加入了新的GMSMapView中,在現有GMSMapView中上述視圖控制器使用代碼所有正在使用IBOutlet中

更新的代碼被配置:

@IBOutlet weak var mapSubView: GMSMapView! 

override func viewDidLoad() { 


    mapSubView.myLocationEnabled = true 
    mapSubView.camera = GMSCameraPosition(target: CLLocationCoordinate2D(latitude: 13.009047, longitude: 77.652949), zoom: 15, bearing: 0, viewingAngle: 0) 


    // Creates a marker in the center of the map. 
    let marker = GMSMarker() 
    marker.position = CLLocationCoordinate2D(latitude: 13.009047, longitude: 77.652949) 
    marker.title = "Bangalore" 
    marker.snippet = "India" 
    marker.map = mapSubView 
} 

谷歌地圖iOS版SDK文檔:GMSCameraPosition Class Reference

谷歌地圖iOS版SDK文檔:這裏 GMSMarker Class Reference

+0

謝謝@Dravidian。但是,你能解釋一下這裏發生了什麼嗎?此外,我的標記也不顯示 –

0

罪魁禍首是:

marker.map = self.mapSubView 

而且,沒有必要:

marker.map = mapView 

將其替換創建mapView

下面是完整的代碼,這應該工作:

let camera = GMSCameraPosition.cameraWithLatitude(13.009047, longitude: 77.652949, zoom: 6) 
    self.mapSubView.camera = camera 
    self.mapSubView.myLocationEnabled = true 

    marker.position = CLLocationCoordinate2D(latitude: 13.009047, longitude: 77.652949) 
    marker.title = "Bangalore" 
    marker.snippet = "India" 
    marker.map = self.mapSubView 

重要變化

self.mapSubView.camera = camera 
marker.map = self.mapSubView