-1
最近「FusedLocationApi」が推奨されなくなりました。私のコードは正常に動作し、私の活動は場所を取得しますが、マーカーを設定しようとすると動作しません。私はとても混乱しています、誰かが私がどこに行方不明を見つけるのを助けることができますか?Googleマップのマーカーが動作しない
ここに私のコード。
class DealerMapsActivity : FragmentActivity(),
OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener{
private val TAG = "DealerMapsActivity"
private var mMap: GoogleMap? = null
private var mGoogleApi: GoogleApiClient? = null
private lateinit var binding: ActivityDealerMapsBinding
private var myLastLocation: Location? = null
private var myMarker: Marker? = null
private var mFusedLocationClient: FusedLocationProviderClient? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.d(TAG,"onCreate")
binding = DataBindingUtil.setContentView(this,R.layout.activity_dealer_maps)
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
}
この場合、LocationCallbackを使用して場所を取得します。
private var mLocationCallback: LocationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult?) {
for (location in locationResult!!.locations) {
Log.i(TAG, "accuracy: "+ location.accuracy + " Location: " +
location.latitude + " " + location.longitude)
myLastLocation = location
}
SetMapMarker()
if(myLastLocation!!.accuracy > 0) {
Log.i(TAG, "set accuracy into text: "+ myLastLocation!!.accuracy)
binding.vAccurate.text =
"Accurate to "+myLastLocation!!.accuracy.toInt().toString()+ " meters"
}
}
}
ここで私のコードはマップマーカーを設定します。
fun SetMapMarker(){
Log.i(TAG, "set marker")
if(myMarker != null){
myMarker!!.remove()
}
val latlng = LatLng(myLastLocation!!.latitude, myLastLocation!!.longitude)
if(myMarker == null){
myMarker = mMap!!.addMarker(
MarkerOptions()
.position(latlng)
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
)
}
myMarker!!.snippet = "ok"
myMarker!!.title = "i'm here"
val camera = CameraPosition
.builder()
.target(latlng)
.zoom(16.0F)
.build()
mMap!!.animateCamera(CameraUpdateFactory.newCameraPosition(camera))
}
}
あなたはmyMarkerをチェックしましたか?remove()文はmyMarkerをnullに設定していますか?いいえの場合はnullを設定する必要があります。 –
ya、私は私のマーカーを削除した後、私のマーカーの位置を更新したいので、私のマーカーを削除しますmymarker == nullの場合はマーカーを再度設定します – Frendi
if(myMarker == null){調子 ? –