最近我找到了#34; FusedLocationApi"已弃用,所以我尝试修复有关谷歌文档的应用程序。我的代码工作正常,我的活动得到位置,但是当我尝试设置标记时,它不起作用。我很困惑,任何人都可以帮我找到我失踪的地方吗?
这是我的代码。
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))
}
}
答案 0 :(得分:0)
fun SetMapMarker(){
val latlng = LatLng(myLastLocation!!.latitude, myLastLocation!!.longitude)
Log.i(TAG, "set marker")
if(myMarker != null){
myMarker!!.position = latlng
}
if(myMarker == null){
myMarker = mMap!!.addMarker(
MarkerOptions()
.position(latlng)
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
)
// you can set title and message only one time not required all time (if you want to change message every time then write these line outside if statement)
myMarker!!.snippet = "ok"
myMarker!!.title = "i'm here"
}
val camera = CameraPosition
.builder()
.target(latlng)
.zoom(16.0F)
.build()
mMap!!.animateCamera(CameraUpdateFactory.newCameraPosition(camera))
}
希望能帮助别人。