0

试了几个零散的教程,这个问题都没有解决

我在 Fragment 中使用谷歌地图 v2 构建。

在这里我尝试使用 OnMarkerClickListener 但该功能无法执行

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    // Inflate the layout for this fragment
    var view = inflater.inflate(R.layout.fragment_maps, container, false)

    //Init Fragment
    activity?.title = arguments?.getString("fragmentName")
    view.setBackgroundColor(Color.WHITE)

    fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(requireActivity())

    Timer("initData", false).schedule(5000){ initData() }
    
    //Init Map
    setupMap(view, savedInstanceState)
    return view
}

设置地图

private fun setupMap(view: View, savedInstanceState: Bundle?){
    mMapView = view.findViewById(R.id.mapView)
    mMapView.onCreate(savedInstanceState)
    mMapView.onResume()

    try {
        MapsInitializer.initialize(requireActivity().applicationContext)
    } catch (e: Exception) {
        e.printStackTrace()
    }

    mMapView.getMapAsync(this)
}

OnMapReady

override fun onMapReady(mMap: GoogleMap) {
    googleMap = mMap
    layMap(mMap)
    initMap(mMap)
    myLocation(mMap)
    initArea(mMap)
    initCamera(mMap)
    setupUI(mMap)
    googleMap.setOnMarkerClickListener {
        Toast.makeText(activity, "imhere", Toast.LENGTH_SHORT).show()
        Log.d("imhere", "imhere")
        true
    }
}

在此 OnMarkerClickListener 部分中,它在片段中根本不起作用,但是在尝试使用此 Activity 时它会起作用

4

1 回答 1

-1

它应该像

googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                @Override
                public boolean onMarkerClick(Marker marker) {
                    // Triggered when user click any marker on the map
                    return true;
                }
            });
于 2021-01-21T13:32:51.567 回答