简体   繁体   中英

G Maps Android Java, last known location marker color different from Marker for location changed,but showing same marker color on running app

//do focus on the last else block of the code and the OnLocationChanged method(which is inside of the OnMapReady() method)

this else block(contains the code fo rthe lastknown location method) and the onLocationChanged method

contains the code for the location which is being changed (here by passing from the extended controls avaialable with the emulator,ie the box opens on clicking the downmost option in the emulator.)

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
    LocationManager locationManager;
    LocationListener locationListener;
    private GoogleMap mMap;

    @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 onMapReady(final GoogleMap googleMap) {
        mMap = googleMap;
        locationManager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
        locationListener=new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                mMap.clear();
                LatLng place = new LatLng(location.getLatitude(), location.getLongitude());
                mMap.addMarker(new MarkerOptions().position(place).title("Marker in Sydney").icon(BitmapDescriptorFactory.defaultMarker(HUE_YELLOW)));
                mMap.moveCamera(CameraUpdateFactory.newLatLng(place));
                mMap.animateCamera(CameraUpdateFactory.zoomTo(3));
            }

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

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

            }
        };
        // Add a marker in Sydney and move the camera
        if(Build.VERSION.SDK_INT<23)
        {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
        }else

            if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED)
            {
                ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
            }
            else
            {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
                Location lastknownlocation=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                LatLng position=new LatLng(lastknownlocation.getLatitude(),lastknownlocation.getLongitude());
                mMap.addMarker(new MarkerOptions().position(position).title("The Location  is"));
                mMap.moveCamera(CameraUpdateFactory.newLatLng(position));
        }
    }
}

Add the yellow icon to your second marker just like you did with the first one, like this:

} else {
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    Location lastknownlocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    LatLng position = new LatLng(lastknownlocation.getLatitude(), lastknownlocation.getLongitude());
    mMap.addMarker(new MarkerOptions().position(position).title("The Location  is").icon(BitmapDescriptorFactory.defaultMarker(HUE_YELLOW)));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(position));
}

Screenshot:

在此处输入图片说明

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM