【问题标题】:How can users add markers by clicking on google maps android?用户如何通过点击 google maps android 添加标记?
【发布时间】:2020-03-02 18:46:51
【问题描述】:

我是 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, 0, this);
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 6000, 0, 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) {

    }
}

【问题讨论】:

    标签: java android google-maps marker


    【解决方案1】:

    您需要致电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();

    希望这会有所帮助! :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-28
      • 1970-01-01
      • 2018-11-01
      • 2015-05-14
      相关资源
      最近更新 更多