Fetching my location with setMyLocationEnabled() method - android

I am trying to get my location in a map using the setMyLocationEnabled() method. There is no problem with this method, I have also used setMyLocationButtonEnabled() and everything is good.
But when I click on the button in the map, I don't get the "blue dot" that indicates my location.
Can you tell me what the problem could be?

The setMyLocationEnable won't work alone. you have to put requestLocationUpdates in the onResume.
#Override
public void onResume() {
super.onResume();
if (isGPSEnabled) {
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, time, distance, this);
}
if (isNetworkEnabled) {
locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, time, distance, this);
}
map.setLocationSource(this);
}

First, you should either declare an anonymous inner class, that implements Google.OnMyLocationChangeListener, or you could just implement it in your main class. Let's say the instance of your map is called map. You will have to set the listener, like this: map.setOnMyLocationChangeListener(this). Then, you will have to do something like this:
#Override
public void onMyLocationChange(Location location) //inherited from the interface you implemented
{
//do your thing, the variable "location" holds everything you need
}

Related

GeoLocation issues?

How can I change the LocationManager to get just one location? The app keeps updating the location.
public void onClick(View view)
//this make the app keep update
locationManager.requestLocationUpdates("gps", 0, 0, listener);
The first approach would be to use locationManager.requestSingleUpdate() instead of locationManager.requestLocationUpdates()
OR
the second solution is to unregister the listener immediately after you received the first update, as follows:
#Override
public void onLocationChanged(Location location) {
locationManager.removeUpdates(this);
// do something with the received location
}

Android GoogleMaps slow onMapClick response

I set a click listener (GoogleMap.OnMapClickListener) for GoogleMap object in the onMapReady() method like this.
#Override
public void onMapReady(GoogleMap map) {
addClickListener(map);
}
Here is the addClickListener():
private void addClickListener(GoogleMap map) {
map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng point) {
Log.d("ON_MAP_CLICK", "1. OnMapClick called");
handleOnMapClick(point);
}
});
}
Since I set the map ready in the onMapReady method it is logical that I also add the click listener there.
The problem is that the delay for clicking on the map and the code to register that click is around 1 second while is should be instant.
Problem: The problem is not the time it takes to call addClickListener(GoogleMap) but the time that onMapClick() registers a click on the map (around 1 second).
Why is it so slow?
I am using GoogleMap V2
Thanks!
It is obvious that addClickListener method is called after 1 second because onMapReady call back will called only after intializing the map. So it take some delay initially for calling your addClickListener method.
So you will observe this delay only for the first time not all the time and it is working as like it is expected, No changes required in your code
onMapReady will call after the map is initialized, i had a project that took a while to load the map making all the functions "slow".
You can try to load the map earlier to prevent that by adding this to the Splashscreen or Activity.
new Thread(new Runnable() {
#Override
public void run() {
try {
MapView mv = new MapView(getApplicationContext());
mv.onCreate(null);
mv.onPause();
mv.onDestroy();
}catch (Exception ignored){
}
}
}).start();

Is it necessary to use LocationManager.requestLocationUpdates and LocationManager.removeUpdates() for Google Maps?

I created an app with an GoogleMapView. In order to reduce battery consumption, I want to stop the location requests, when the app is not opened. I found that this can be done with the LocationManager. For example like this:
#Override
public void onPause() {
locationManager.removeUpdates(this);
super.onPause();
}
#Override
public void onResume() {
locationManager.requestLocationUpdates(locationManager.getBestProvider(criteria, true), minUpdateTime, minUpdateDistance, this, null);
super.onResume();
}
But at the moment I am not using the LocationManager at all and the GoogleMapsView still manages to get the current location. I merely need to call mMap.setMyLocationEnabled(true); in my OnMapReadyCallback() function. So it seems like the GoogleMapsView implements its own sort of LocationManager which leads to the question, if it is necessary to create a LocationManager just to tell it to stop updating when the app is closed, or is it enough to call mMap.setMyLocationEnabled(false); in my onPause() function?

can not setInterval in onLocationChanged

I am trying the 'LocationUpdates' sample from http://developer.android.com/training/location/receive-location-updates.html . This application gets and prints location notifications.
I am trying to change the interval of the location updates according to my latest location.
So - I had added mLocationRequest.setInterval() into onLocationChanged
The result is very wrong. My application is bombarded with many location updates (few a second!!!!)
My only change to the sample is this:
private int x=0;
#Override
public void onLocationChanged(Location location) {
// Report to the UI that the location was updated
mConnectionStatus.setText(R.string.location_updated);
// In the UI, set the latitude and longitude to the value received
mLatLng.setText(String.valueOf(x++));
mLocationRequest.setInterval(1000); // Change 1
mLocationClient.requestLocationUpdates(mLocationRequest, this); // Change 2
}
How can I change the interval inside onLocationChanged ?
I think that the problem is that requestLocationUpdates resets the last request, and then immediately sends another notification. so a loop is created. (faster than the fastest interval). so I need a reliable way to change the interval of a 'live' LocationRequest
You are not supposed to call mLocationClient.requestLocationUpdates(mLocationRequest, this); inside onLocationChanged(Location location)
since you are registering the listener again, and you will get the first call immediately.
so what i would do would be:
dont call mLocationClient.requestLocationUpdates(mLocationRequest, this); and see if anyways mLocationRequest.setInterval(1000); is taking effect
if this doesnt work, try to unregister the listener, and then use a trick to wait before registering it again with the new settings, something like:
Handler h = new Handler();
#Override
public void onLocationChanged(Location location) {
//... all your code
mLocationRequest.setInterval(1000);
mLocationClient.removeLocationUpdates(LocationListener listener)
h.postDelayed (new Runnable(){
public void run(){
mLocationClient.requestLocationUpdates(mLocationRequest, YOUROUTTERCLASS.this);
}
}, 1000);
}
So during one second there is not registered listener, so you wont get any updated, and after that, the listener is registerered with that interval.
Try using mLocationRequest.setFastestInterval(long millis)
As mentioned in developer.android.com :
This allows your application to passively acquire locations at a rate faster than it actively acquires locations, saving power. Unlike setInterval(long), this parameter is exact. Your application will never receive updates faster than this value.
Try to use :
mLocationRequest.requestLocationUpdates("gps", 1000, 0, this);
However I don't agree to do a requestLocationUpdates in onLocationChanged event; In my opinion should be setted outside onLocationChanged Event...

The locationListener is only invoked once

I want to send some data along with coordinates to a remote server each 10 seconds. I thought, that the best match would be the
public void onCreate( Bundle savedInstanceState ) {
//snip
locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 10000, 0, new SendingLocationListener() );
}
in the listener I have the following code:
public void onLocationChanged( Location location ) {
if( null == location ) return;
TrackerNotifierTask task = new TrackerNotifierTask();
task.execute( location );
}
the TrackerNotifierTask uses the httpclient in it's doInBackground() method, so it's pretty simple.
Now, if I start the activity, I can see that the onLocationChanged() gets executed and the data hits the remote server successfully. But only once! No matter what I do later, changing coords or anything, the task does not get called.
Is this the right way of implementing such thing in android or shall I resort to some background-service?
the link from Shrikant gave me some hints on class-structure of the LocationListener implementation.
The class must not necessarily be defined as an inner anonymous class. I defined it as inner, but named one and it also worked.
The trick is, that the listener instance must be declared as a field:
private LocationListener listener;
#Override
public void onCreate( Bundle savedInstanceState ) {
//snip
locationManager = (LocationManager)this.getSystemService( Context.LOCATION_SERVICE );
listener = new MyLocationListener( SomeActivity.this );
locationManager.requestLocationUpdates( provider, 1000, 0, listener );
}
Otherwise it will be garbage-collected after the 1st run, if defined like this:
locationManager.requestLocationUpdates( provider, 1000, 0, new MyLocationListener() );
That was one half of the solution. The other part - about minTime - remains unresolved. Maybe it has something to do with emulator... I'll post the missing part as soon as I find the way.
UPDATE:
Seems like, that on a real device the minInterval seems to cause the listener to fire, no matter if the coords changed or not
Please see this.
It works as expected when LocationListener is implemented as an anonymous class.
I'v a similar problem,
when I use
private LocationListener mLocationListener1 = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
}
}
in my class,
onLocationChanged is only invoked once,
but after I add "final" to
private final LocationListener mLocationListener1 = new LocationListener() {
The problem is solved. (in my case, used by forground service)
It seems like
if the variable is not defined with final keyword,
after it was called once ,
it would be garbage collected and will not be called again...

Categories

Resources