2


After trying for long..am facing problem in getting a user's current GPS location fix.

So, I thought that if can we somehow programatically get a location fix using pre-installed Google maps app's Current Location fixer?

If I am able to fetch the location co-ordinates in the background..is this possible?
So, is it possible to somehow fetch the current location co-ordinates by using an intent or broadcast receiver?(supposing that the maps app is broadcasting it.. ? )

Update #2 : Finally..I got it to work..

Update: My earlier code is here(PasteBin Link)

Any advice is welcome.. Thanks..

Community
  • 1
  • 1
beerBear
  • 969
  • 2
  • 17
  • 41
  • I haven't got your issue... Are you trying to get user's current location through GPS? If yes, then its "very simple": make a class extend from LocationListener and override onLocationChanged method. Have you already took your MD5 API key to use Google Maps? – Marcelo Feb 04 '13 at 19:20
  • @mthama yes and yes.. :D Infact here is my [orig ques](http://stackoverflow.com/questions/14692202/unable-to-get-current-gps-location-co-ordinates). And I have already gone through the drill :D keystore..console..api key..pftt :p I am missing something somewhere..or quite possibly..my Phone's bugged :| But how in this world..could my phone's Google maps app locks on to a GPS satellite just fine? :O BTW have you tried getting a GPS lock on HTC desire s too? or wa it on android 2.3.3 based device? :| (in real time..no DDMS thingy) – beerBear Feb 04 '13 at 19:26
  • 1
    Your code is to complicated to see if something is not correct. All I can say you can use Criteria, as well as just requesting the GPS_PROVIDER without it. And "getMyLocation" is bad class name. Class names in Java should start with upper case letter and must not be verb. getMyLocation is a good method name, but class should be named as LocationRetreiver or something :) And remember that to get a GPS lcoation fix you might want to be outside, or better use FakeGPS https://play.google.com/store/apps/details?id=com.lexa.fakegps - if all works fine you should get the location fix immediately. – Yaroslav Mytkalyk Feb 05 '13 at 09:40
  • @DoctororDrive ahh :) nvm I fixed it..its working fine..(question contains an update..more details will follow. Thanx for helping me out :) ) – beerBear Feb 05 '13 at 09:49

2 Answers2

2

Ok codebreaker, here is how I implemented my location fetcher:

// the listener to listen to the locations
private LocationListener listener = null;
// a location manager
private LocationManager lm  = null;
// locations instances to GPS and NETWORk
private Location myLocationGPS, myLocationNetwork;

// instantiates fields
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
myLocationNetwork = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
myLocationGPS = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
listener = new myLocationListener();

// the listener that gonna notify the activity about location changes
public class myLocationListener implements LocationListener {
    @Override
    public void onLocationChanged(Location location) {
        // "location" is the RECEIVED locations and its here that you should proccess it

        // check if the incoming position has been received from GPS or network
        if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            lm.removeUpdates(this);
        } else {
            lm.removeUpdates(listener);
        }
    }
    @Override
    public void onProviderDisabled(String provider) {
        lm.removeUpdates(this);
        lm.removeUpdates(listener);         
    }
    @Override
    public void onProviderEnabled(String provider) {
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
}

As you can see, there's not so much trickys. At the time that you instantiates the listener, you should see the GPS icon on the top of your Android device. Also, whenever your position changes (i.e. as you walk with your device), the OnLocationChanged method will be called.

Also, it is interesting to say that if you want to just get you locations, there are several ways to do it, all of then with different speeds of return and different acurracies. Please, check also GoogleGLM (a request to http://www.google.com/glm/mmap, that returns a json encoded strign with your position) services, triangulations and location by network. In the above snippets I've showed how to get location by either GPS and network. Hope that it has been of some help... :)

Community
  • 1
  • 1
Marcelo
  • 2,075
  • 5
  • 21
  • 38
  • In the line `if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER) && (isNetOrGPs%4 == 0)) {` this: `isNetOrGPs%4 == 0` ? My earlier code is [here-PasteBin Link](http://pastebin.com/Wm6h9VFV) – beerBear Feb 04 '13 at 19:57
  • You can just ignore it. Sorry for that: I've copy/paste the snippet from my project and missed this little thing. :) – Marcelo Feb 04 '13 at 20:01
  • The most important part is to override OnLocationChanged. You can put a Toast.makeText method to toast your position as feedback. ;) – Marcelo Feb 04 '13 at 20:03
  • Please see my longer version of the code ^ (link in updated question and also in comment above (PasteBin link)). I will be trying the code on a different device tommorow..lets hope it works fine. :| – beerBear Feb 04 '13 at 20:07
  • If you are getting hard-times to make this implementation then please, email me: hama.marcelo.ti@gmail.com. I can send you a little runnable example with location fetch. – Marcelo Feb 04 '13 at 20:09
  • +1 yep..will do (though can't be online for long enough..gotta take a nap..zzzZZZZ) :) Thanks in advance :D – beerBear Feb 04 '13 at 20:15
0

You can use the passive location provider, but it does not explictly mean it will fetch the location from GoogleMaps. Also, the GoogleMaps should be running in order to receive updates. To receive location updates from GoogleMaps you also must have permission to ACCES_FINE_LOCATION. Also you can't start GoogleMaps and force it to get location updated without user interaction. If you need precise user location there is no other way than using GPS_PROVIDER.

http://developer.android.com/reference/android/location/LocationManager.html#PASSIVE_PROVIDER

Also you might want to read http://developer.android.com/guide/topics/location/strategies.html http://developerlife.com/tutorials/?p=1375

Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99
  • Ok..I am getting location from a network just fine (actually haven't really put that code here on SO) but I am more into getting a neat..precise location fix instead of having an accuracy of 800 mtrs/fts. :( Isn't there a way I can run Google maps in background (fetching co-ords in an async task? :D ) and after google maps have got a satellite fix then just fetch the geo fix from maps somehow and reflect it into my app? :| – beerBear Feb 04 '13 at 19:34
  • I have tried a heavily modified criteria in my other* code (such as disabling bearing and altitude and even setting power requirements to high..but sadly that just gets me a network fix :( no GPS (in that code even the GPS icon isn't blinking..isn't visible but when I check the GPS checkbox in the notifications area..its selected (I explicitly fire an intent to switch on the GPS before trying a geo fix) ). – beerBear Feb 04 '13 at 19:38
  • GPS will not always return you the location fix. You must be outside in order to get guaranteed relatively fast fix. You can use LocationProvider.getLastKnownLocation() to get previously saved result (can be null, if nothing was updated since phone restart) meanwhile waiting for a new fix. – Yaroslav Mytkalyk Feb 04 '13 at 19:40
  • Yep..this particular permission including others (like internet, coarse location too) are already defined in the manifest file. Also, can't I simulate a user's click on Google maps "get my location" button under settings? Is this part of google maps source code open for public? Can I have a peek into it? – beerBear Feb 04 '13 at 19:42
  • You can't simulate click's on other application buttons. All you can do is start GoogleMaps by adding a geo: scheme Uri to an ACTION_VIEW Intent. But it will prompt user and bring GoogleMaps to front meanwhile pausing your Acitivty. And I'm afraid GoogleMaps is not open source. And event not installed on all devices. – Yaroslav Mytkalyk Feb 04 '13 at 19:46
  • There we go..that explains Google maps's quick geo fix :| (no offence Google :D ) I will be posting my heavily modified code soon(the one in which I am using a criteria). – beerBear Feb 04 '13 at 19:49
  • please see my earlier code's pastebin link # Question updated :) – beerBear Feb 04 '13 at 20:01