Im having trouble passing the current location to my LatLng variable to show on my google map. Im not sure how to pass the value from my override statement in my location listener method. Im trying to implement an application that simply gets the GPS coordinates and places a marker on the map.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
//obtain location
listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
double lat = location.getLatitude();
double lng = location.getLongitude();
LatLng appoint = new LatLng(lat,lng);
return appoint;
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
// 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;
double lat = 34.014783;
double lng = -84.571381;
// Add a marker in Appoint from database and move the camera
LatLng appoint = new LatLng(lat,lng);
mMap.addMarker(new MarkerOptions().position(appoint).title("Where the wild gays are"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(appoint));`
Well, onLocationChanged
doesn't return anything void
so you cannot return the LatLng
object, but you can add the marker
to the mMap
object (as long as it is not null).
Eg
public void onLocationChanged(Location location) {
double lat = location.getLatitude();
double lng = location.getLongitude();
LatLng appoint = new LatLng(lat,lng);
if(mMap != null) {
mMap.addMarker(new MarkerOptions().position(appoint).title("Where the wild gays are"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(appoint));
}
}
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.