2017-07-29 57 views
2

你好,我想在科特林來實現Google Maps,但在這些代碼我getMapAsync是沒有得到所謂的地圖完美展現,但標記不露面科特林與地圖(標記不顯示)

var mapFragment : SupportMapFragment?=null 
    mapFragment= fragmentManager.findFragmentById(R.id.map) as SupportMapFragment? 
    mapFragment?.getMapAsync { 
     val sydney = LatLng(22.30, 73.20) 
     mMap!!.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney")) 
     mMap!!.moveCamera(CameraUpdateFactory.newLatLng(sydney)) 


     Toast.makeText(activity,"dfgdg",Toast.LENGTH_LONG).show() } 
+0

'mapFragment'可能是零,這意味着'fragmentManager'無法找到它。你是否在代碼中的某個地方初始化了'SupportMapFragment'? –

+0

其實地圖在屏幕上顯示,但標記不添加 –

+0

你可以顯示你的佈局XML嗎?或者你在某處調用'SupportMapFragment()'(構造函數)? –

回答

1
  1. 您使用了錯誤的地圖實例。
  2. 悉尼緯度經度是-33.8479731,150.6517908
mapFragment?.getMapAsync { 

    map -> // <- this is the map that you got async not mMap    
     val sydney = LatLng(-33.8479731, 150.6517908) 
     map.addMarker(... 
     map.moveCamera(... 
}