用户如何通过单击Google Maps android来添加标记?

我是Android编程的初学者。我已经看过类似的问题和答案,但是我仍然不知道为什么这行不通。当我在模拟器上尝试此操作并单击一个位置时,没有标记出现。这是我的代码:

public class Mapactivity extends AppCompatactivity implements LocationListener,OnmapReadyCallback,GoogleMap.OnmapClicklistener {

    private MapFragment mapFragment;
    private GoogleMap googleMap;
    Locationmanager locationmanager;
    ArrayList<LocationProvider> providers;
    private boolean isUpdatePosition;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        this.isUpdatePosition = true;
        FragmentManager fragmentManager = getFragmentManager();
        mapFragment = (MapFragment) fragmentManager.findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

    } // End onCreate

    private void mapDisplayPosition(){
        if ( ContextCompat.checkSelfPermission( this,android.Manifest.permission.accESS_FINE_LOCATION ) == PackageManager.PERMISSION_GRANTED ) {
            this.googleMap.setMyLocationEnabled(true);

            locationmanager = (Locationmanager)getSystemService(Context.LOCATION_SERVICE);
            List<String> names = locationmanager.getProviders(true);

            providers = new ArrayList<LocationProvider>();
            for(String name : names)
                providers.add(locationmanager.getProvider(name));

            Criteria critere = new Criteria();

            // Pour indiquer la précision voulue
            // On peut mettre accURACY_FINE pour une haute précision ou accURACY_COARSE pour une moins bonne précision
            critere.setaccuracy(Criteria.accURACY_FINE);

            // Est-ce que le fournisseur doit être capable de donner une altitude ?
            critere.setaltitudeRequired(true);

            // Est-ce que le fournisseur doit être capable de donner une direction ?
            critere.setBearingRequired(true);

            // Est-ce que le fournisseur peut être payant ?
            critere.setCostAllowed(false);

            // Pour indiquer la consommation d'énergie demandée
            // Criteria.POWER_HIGH pour une haute consommation,Criteria.POWER_MEDIUM pour une consommation moyenne et Criteria.POWER_LOW pour une basse consommation
            critere.setPowerRequirement(Criteria.POWER_HIGH);

            // Est-ce que le fournisseur doit être capable de donner une vitesse ?
            critere.setSpeedRequired(true);

            locationmanager.requestLocationUpdates(Locationmanager.GPS_PROVIDER,6000,this);
            locationmanager.requestLocationUpdates(Locationmanager.NETWORK_PROVIDER,this);
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude());
        float zoom = 15;




        MarkerOptions markerOptions = new MarkerOptions();

        markerOptions.position(latLng);
        markerOptions.title(latLng.latitude + " : " + latLng.longitude);
        googleMap.clear();
        if(this.isUpdatePosition){
            googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
            this.isUpdatePosition = false;
        }

        googleMap.addMarker(markerOptions);

    }

    @Override
    public void onStatusChanged(String s,int i,Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }

    @Override
    public void onmapReady(GoogleMap googleMap) {
        this.googleMap = googleMap;

        if ( ContextCompat.checkSelfPermission( this,android.Manifest.permission.accESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED ) {
            activityCompat.requestPermissions( this,new String[] {  android.Manifest.permission.accESS_FINE_LOCATION  },2);
        }
        mapDisplayPosition();



    }


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

        mapDisplayPosition();
    }

    @Override
    public void onmapClick(LatLng latLng) {

    }
}
prince373 回答:用户如何通过单击Google Maps android来添加标记?

您需要致电setOnMapClickListener才能在地图上使用OnMapClickListener

this.googleMap.setOnMapClickListener(this);

将其添加到onMapReady函数中,如下所示:

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

    if (ContextCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},2);
    }

    this.googleMap.setOnMapClickListener(this);

    mapDisplayPosition();
}

然后在onMapClick函数中添加标记,如下所示:

@Override
public void onMapClick(LatLng latLng) {
    MarkerOptions marker = new MarkerOptions()
            .position(latLng);
    this.googleMap.addMarker(marker);
}

现在,您应该可以在地图上的任意位置单击以放置标记。但是,请注意,您正在onLocationChanged函数中通过以下行清除地图:

googleMap.clear();

这会使新标记在几秒钟内消失。假设这不是预期的行为,请通过注释掉它来解决,例如//googleMap.clear();

希望这会有所帮助! :)

本文链接:https://www.f2er.com/3153295.html

大家都在问