Stopping OnMyLocationChangeListener - android

I want to stop the LocationListener once it has the current location and zoomed to it.
Can you please help a newbie?
Here's my code:
#Override
public void onMyLocationChange(Location location) {
// Getting latitude of the current location
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Showing the current location in Google Map
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
}

Set the map location listener to null to turn it off.
mMap.setOnMyLocationChangeListener(null);

Sadly,the above solution did not work for me,
I had to add:
mMap.setMyLocationEnabled(false);

Related

How to make the onLocationResult function work?I

I am trying to show my current location and store the location to Firebase and to place a marker on my current location.
i searched in google and even stack overflow t couldn't find anything related to my problem.
Also i read googles documentation on its maps sdk but didn't understand it.
LocationCallback mLocationCallback = new LocationCallback() { //NEW onLocation changed
#Override
public void onLocationResult(LocationResult locationResult) {
for (Location location : locationResult.getLocations()) {
mLastLocation = location;
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));//1-21
}
}
};
I expect to see a blue dot in my app but instead only see the map without any markers or even without my current location
public fun placeMarkerOnMap(location: LatLng, drawableResInt: Int? = null): Marker? {
val markerOptions = MarkerOptions().position(location)
val marker = googleMap?.addMarker(markerOptions)
if (drawableResInt != null) {
marker?.setIcon(BitmapDescriptorFactory.fromResource(drawableResInt))
}
return marker
}
Your code miss the part of adding marker.You can add marker to particular LatLng with/without custom image.Then only, move the camera.
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng))
Add Marker By doing following
double latitude = location.getLatitude();
double longitude = location.getLongitude());
LatLng latlng= new LatLng(latitude, longitude); ////your lat lng
mMap.addMarker(new MarkerOptions().position(latlng));
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(posisiabsen, 65);
mMap.animateCamera(cameraUpdate);

google Maps v2 API for android location return null [duplicate]

This question already has answers here:
My current location always returns null. How can I fix this?
(7 answers)
Closed 7 years ago.
Hi I need some help with this, I have already looked and try many different things. I'm trying to get my current location on android but since the location comes back null my app crashes. I really need help with this please.
I don't know if I'm being clear in here but everytime I call getLastKnownlocation it comes back null, so when I try to get the double lat= location.getLatitude() same for the longitude it won't return anything and there is where my app crashes.
Help...
Here a piece of code
mMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
double lat = location.getLatitude();
double lng = location.getLongitude();
LatLng latLng = new LatLng(lat, lng);
at latitude is where it stops.
Do you retrieve the GoogleMap object from the fragment before calling those functions?
Something like:
//get map fragment from the static layout (in this case it has id = "map")
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
//get GoogleMap object, then you can start using it, for example enabling the location
map = mapFragment.getMap();
map.setMyLocationEnabled(true);
Thank you all for your help I appreciate it, I have already solved my problem.
Here a piece of code that helped me get ti done:
//I needed to have this line before everything else for me to get my current
//location from getLastKnownLocation method.
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker").snippet("Snippet"));
//And the all the codes I had before
mMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
double lat = location.getLatitude();
double lng = location.getLongitude();
LatLng latLng = new LatLng(lat, lng);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
mMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.title("Here"));

Set marker to the center of circle (current location)

I use Google Map Android API v2 in my app and I have a little problem as in the picture below but have no ideas how to fix it. I set marker to current location of device but its now in the center of circle. I want to set marker to the center of the circle. Is it possible or not?
Thanks for any help!
Code that I used:
// Showing status
if(status!= ConnectionResult.SUCCESS){ // Google Play Services are not available
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
dialog.show();
}else { // Google Play Services are available
// Getting reference to the SupportMapFragment of activity_location.xml
SupportMapFragment mSupportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
// Getting GoogleMap object from the fragment
mGoogleMap = mSupportMapFragment.getMap();
// Enabling MyLocation Layer of Google Map
mGoogleMap.setMyLocationEnabled(true);
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = mLocationManager.getBestProvider(criteria, true);
// Getting Current Location
Location location = mLocationManager.getLastKnownLocation(provider);
if(location!=null){
onLocationChanged(location);
}
mLocationManager.requestLocationUpdates(provider, 20000, 0, this);
// Getting latitude of the current location
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng mLatLng = new LatLng(latitude, longitude);
//Add Marker to current location of devise
mGoogleMap.addMarker(new MarkerOptions()
.position(mLatLng)
.title("Geolocation system")
.snippet("Your last current location which was available!"));
// .icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_location)));
// Showing the current location in Google Map
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(mLatLng));
// Show Zoom buttons
mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
// Turns on 3D buildings
mGoogleMap.setBuildingsEnabled(true);
// Zoom in the Google Map
mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(14));
You are getting your location using GPS and network. these are not giving your correct location and you are using:-
mGoogleMap.setMyLocationEnabled(true);
this showing your location using google map api. so the loaction by you and google are different .so circle and marker are showing different location.
so you can use google's Fuesed Location client for this
Visit this for more help.
https://developer.android.com/reference/com/google/android/gms/location/FusedLocationProviderApi.html

how to draw dynamic travel path on google map in android

i have integrated google map in android. and i am getting current location using GPS. on Onlocationchanged i am updating the current location.
My doubt is i am on the Google map UI . i wants to walk or drive that time from my current location as starting point and i wants to start draw the line on map dynamically.
i searched on net for long time but i am not getting any solution . here is my code please anyone help me to over come this problem.
private void initalizeMap()
{
// Google Play Services are available
// Getting GoogleMap object from the fragment
googleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
if(gps.canGetLocation())
{
double latitude = Double.valueOf(Helper.loadSavedPreferences(getActivity(), NameConversion.LATITUDE));
double longtitude = Double.valueOf(Helper.loadSavedPreferences(getActivity(), NameConversion.LONGITUDE));
Log.d("getting gps value", ""+ latitude);
Log.d("getting gps Long", ""+ longtitude);
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longtitude);
// create marker
marker = new MarkerOptions().position(latLng).title("You are here!");
// Changing marker icon
// set yours icon here
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_green_point));
// Showing the current location in Google Map
googleMap.setTrafficEnabled(true);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(25));
googleMap.addMarker(marker);
}else{
Toast.makeText(getActivity(), "failed To get the location", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onLocationChanged(Location location) {
double latitude = gps.getLatitude();
// Getting longitude of the current location
double longitude = gps.getLongitude();
// Creating a LatLng object for the current location
Log.d("latitude map Class", "latitude: " + gps.getLatitude());
Log.d("longitude Map CLass", "longitude: " + gps.getLongitude());
LatLng latLng = new LatLng(latitude, longitude);
// create marker
MarkerOptions marker = new MarkerOptions().position(latLng).title("You are here!");
// Changing marker icon
// set yours icon here
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_green_point));
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(25));
}
and i tried with following links [1 st lnikn second link
In order to get the drive or walking time on Google maps you have to make use of the Direction API's provided by Google. The API call for the Direction would would something like this:
http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los%20Angeles,CA&sensor=false
And you can embed that inside a class where you would make an http call using AsyncTask which would look like this:
GenericUrl url = new GenericUrl("http://maps.googleapis.com/maps/api/directions/json");
url.put("origin", origin);
url.put("destination", destination);
url.put("sensor",false);
Please follow this tutorial or this in order get the direction API (that includes driving, transit. walking and bicycling direction on your maps. )
Hope that would help!!!

Android Google map customize my current location

I already implement Google map to find location and my current position..Position and my current location shows well.But my current location indicates by blue dot(default),I want to customize.I know it will done by marker but not working it...
here is my code..
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
googleMap.getUiSettings().setCompassEnabled(false);
googleMap.getUiSettings().setRotateGesturesEnabled(true);
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Target Location + "+ latitude+" +"+longitude);
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
// adding marker
googleMap.addMarker(marker);
Location myLocation = googleMap.getMyLocation();
if(myLocation!=null)
{
double dLatitude = myLocation.getLatitude();
double dLongitude = myLocation.getLongitude();
Log.i("APPLICATION"," : "+dLatitude);
Log.i("APPLICATION"," : "+dLongitude);
googleMap.addMarker(new MarkerOptions().position(
new LatLng(dLatitude, dLongitude)).title("My Location").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
// mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dLatitude, dLongitude), 8));*/
}
else
{
Toast.makeText(this, "Unable to fetch the current location", Toast.LENGTH_SHORT);
}
}
You set your new marker's position when you initialize your map. But location provider will update your location for finding your real location. So you should track your location and change marker location on every update.

Categories

Resources