【问题标题】:Google Map Marker not working谷歌地图标记不起作用
【发布时间】:2023-03-21 15:27:02
【问题描述】:

最近我发现“FusedLocationApi”已被弃用,所以我尝试修复我的有关 google doc 的应用程序。我的代码工作正常,我的活动得到了位置,但是当我尝试设置标记时,它不起作用。我很困惑,谁能帮我找到我失踪的地方?

这是我的代码。

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。
  • 是的,我删除了我的标记,因为我想更新我的标记位置,如你所见,在我删除我的标记后,如果 mymarker == null,我再次设置我的标记
  • 你检查控制进入 if(myMarker == null){ 条件?
  • 您可以使用 marker.setPosition(latlng) 更新标记位置,而不是删除标记​​span>
  • 哇,它正在工作,谢谢@JRamesh,你拯救了我的一天。

标签: android google-maps data-binding kotlin marker


【解决方案1】:

正如@J Ramesh 建议的那样,非常有趣。我只是用这个替换。

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

希望能帮助别人。

【讨论】:

    猜你喜欢
    • 2019-01-16
    • 1970-01-01
    • 1970-01-01
    • 2015-01-23
    • 2012-11-19
    • 2011-10-16
    • 2011-07-19
    • 2014-01-15
    相关资源
    最近更新 更多