How to add marker on google maps android?

后端 未结 5 2069
一向
一向 2021-01-01 13:20

I am a beginner in Android programming. I already looked at similar questions and answers but I still can\'t figure out why this doesn\'t work. When I try this on the emulat

5条回答
  •  星月不相逢
    2021-01-01 13:36

    This code in MapsActivity works for me :

    public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMapLongClickListener {
    
    private GoogleMap mMap;
    LocationManager locationManager;
    LocationListener locationListener;
    
    public void centreMapOnLocation(Location location, String title){
    
        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
    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,0,0,locationListener);
    
                Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                centreMapOnLocation(lastKnownLocation,"Your Location");
            }
        }
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps2);
        // 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 onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
    
        mMap.setOnMapLongClickListener(this);
    
        Intent intent = getIntent();
        if (intent.getIntExtra("Place Number",0) == 0 ){
    
            // Zoom into users location
            locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
            locationListener = new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    centreMapOnLocation(location,"Your Location");
                }
    
                @Override
                public void onStatusChanged(String s, int i, Bundle bundle) {
    
                }
    
                @Override
                public void onProviderEnabled(String s) {
    
                }
    
                @Override
                public void onProviderDisabled(String s) {
    
                }
            };
    
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
                    Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    centreMapOnLocation(lastKnownLocation,"Your Location");
            } else {
    
                ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
            }
        } else {
    
            Location placeLocation = new Location(LocationManager.GPS_PROVIDER);
            placeLocation.setLatitude(MainActivity.location.get(intent.getIntExtra("Place Number",0)).latitude);
            placeLocation.setLongitude(MainActivity.location.get(intent.getIntExtra("Place Number",0)).longitude);
    
            centreMapOnLocation(placeLocation,MainActivity.places.get(intent.getIntExtra("Place Number",0)));
        }
    
    
    }
    
    
    @Override
    public void onMapLongClick(LatLng latLng) {
        Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
        String adress ="";
        try {
            List
    listaddress = geocoder.getFromLocation(latLng.latitude,latLng.longitude,1); if (listaddress != null && listaddress.size()>0){ if (listaddress.get(0).getThoroughfare() != null){ if (listaddress.get(0).getSubThoroughfare() != null){ adress += listaddress.get(0).getSubThoroughfare() + ""; } adress += listaddress.get(0).getThoroughfare(); } } }catch (Exception e){ e.printStackTrace(); } mMap.addMarker(new MarkerOptions().position(latLng).title(adress)); MainActivity.places.add(adress); MainActivity.location.add(latLng); MainActivity.arrayAdapter.notifyDataSetChanged(); Toast.makeText(this, "Location Saved..!", Toast.LENGTH_SHORT).show(); }

    }

提交回复
热议问题