Good day,
at the moment I am creating an Android application that will show the user their current location on a map. Sadly I can't seem to zoom at the current location of the user in the OnCreate method. It seems to work in a OnMapClick method.
I would like to get rid of the OnMapClick method and show the current location of the user when the map is loaded.
the code below is part of the OnCreate method of the map class
Settings setting = new Settings();
setting.saveSettings(Maps.this);
LocationManager locationManager =(LocationManager) getSystemService(Context.LOCATION_SERVICE);
Here is the code of the OnLocationChanged method
public void onLocationChanged(Location location) {
int lat = (int)(location.getLatitude());
int lng =(int)(location.getLongitude());
LatLng loc = new LatLng(lat,lng);
myMap.moveCamera(CameraUpdateFactory.newLatLngZoom(loc, 15));
myMap.animateCamera(CameraUpdateFactory.zoomIn());
I posses an API key and have the map in my application.
This code may help you...
my listener
private LocationListener listener = new LocationListener() {
@Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } @Override public void onLocationChanged(Location loc) { // will get the location lat long } };
code to start the location manager update listener
LocationManager mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); if (mLocationManager != null) { Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_COARSE); String prividerName = mLocationManager.getBestProvider(criteria, true); if (prividerName != null) { mLocationManager.requestLocationUpdates(prividerName, 30 * 1000, 0, listener);
Once you get the lat/long u can plot the location details as below
GoogleMap googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById( R.id.map)).getMap(); LatLng current = new LatLng(mUserLatitude, mUserLongitude); googleMap.addMarker(new MarkerOptions().position(current).title("Current Location")) .showInfoWindow(); googleMap.getUiSettings().setCompassEnabled(true); googleMap.getUiSettings().setZoomControlsEnabled(true); googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(current, 10));
You probably do not have a fix on the location yet. I would suggest doing
myMap.setMyLocationEnabled(true);
myMap.setOnMyLocationChangeListener(this);
then in you onLocationChanged
put a boolean for a first load or something so it does not go to the location every time the location changes and then do the camera movement in there
I hope this piece of code can help to clearify my question.
the code below is my LocationListener
private LocationListener listener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(Location loc) {
double lat = loc.getLatitude();
double lng = loc.getLongitude();
}
};
the latitude and longitude ares saved in antoher class, the code below show my way of initialzing the map.
String locLat = ((Settings)this.getApplication()).getLat();
String locLng = ((Settings) this.getApplication()).getLng();
double DlocLat = Double.parseDouble(locLat);
double DlocLng = Double.parseDouble(locLng);
myMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
LatLng current = new LatLng(DlocLat,DlocLng);
myMap.addMarker(new MarkerOptions().position(current).title("Current Location")).showInfoWindow();
myMap.setMyLocationEnabled(true);//show the location on the map
myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);//show the regular google maps
myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(current, 10));
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.