首页 > 解决方案 > On Map Long Click listener 没有在我的 G Map 上放置标记,你能帮我吗?

问题描述

当我长按时帮助没有任何反应。另外,我在启动模拟器时收到这些消息,这可能与我的问题有关吗?其他一切似乎都在应用程序中工作。

这是我的代码:

公共类 MapsActivity 扩展 FragmentActivity 实现 OnMapReadyCallback、GoogleMap.OnMapLongClickListener {

@Override
public void onMapLongClick(LatLng latLng) {
    Log.i("Map", "CLICKED");
    mMap.addMarker(new MarkerOptions().position(latLng).title("Your new Memorable Place").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));

    Toast.makeText(getApplicationContext(),
            "New marker added@" + latLng.toString(), Toast.LENGTH_LONG)
            .show();
}

LocationManager locationManager;
LocationListener locationListener;
private GoogleMap mMap;

public void centerMapOnLocation (Location location, String title) {

    if (location != null) {
        LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());

        mMap.clear();
        mMap.addMarker(new MarkerOptions().position(userLocation).title(title));
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 12));
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION ) == PackageManager.PERMISSION_GRANTED){
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, locationListener);
            Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            centerMapOnLocation(lastKnownLocation, "Your Location");
        }
    }
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;



    Intent intent = getIntent();
    if (intent.getIntExtra("placeNumber", 0) == 0){
        //Zoom in on user location

        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                centerMapOnLocation(location, "Your Location");
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {

            }
        };

        if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION ) == PackageManager.PERMISSION_GRANTED){
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, locationListener);
            Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            centerMapOnLocation(lastKnownLocation, "Your Location");
        }else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        }
    }

}

}

标签: javaandroidgoogle-maps

解决方案


我认为您错过了将侦听器添加到地图中。

@Override
public void onMapReady(GoogleMap googleMap){
        mMap=googleMap;
        mMap.setOnMapLongClickListener(this);
        //rest of your code.
}

推荐阅读