0

I want to get my location if phone is connected to Wifi or mobile network. However if phone is on mobile network things dont run as wanted. Nothing is happening until if i enable GPS. Can i get location without enabling GPS or i have to do that ? And if GPS is must, how could i warn user to enable GPS ?

private void setUpMap() {
    // TODO Auto-generated method stub
    mMap.setMyLocationEnabled(true);

    LocationManager manager = (LocationManager)getActivity().getSystemService(FragmentActivity.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = manager.getBestProvider(criteria, true);

    myLocation = manager.getLastKnownLocation(provider);

    latitude = myLocation.getLatitude();
    longitude = myLocation.getLongitude();
    LatLng latlng = new LatLng(latitude, longitude);

    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 14));
}
Andy G
  • 19,232
  • 5
  • 47
  • 69
kort.es
  • 479
  • 2
  • 7
  • 26

2 Answers2

1

Please refer this Demo.

You have to create one GPSTracker Services which is track your last location.

I think 100% solve your issue if you concentrate on demo.

If work like charm then accept answer.

Harshid
  • 5,701
  • 4
  • 37
  • 50
0

You have a very loaded question.

Can i get location without enabling GPS or i have to do that ? 

Of course you can. Location comes from Location Providers. Examples includes Network, Wifi and GPS. You do not need all 3. Just any one will do.

And if GPS is must, how could i warn user to enable GPS ?

You can use the method isProviderEnabled (String provider) under LocationManager Class to check if the GPS Provider is enabled. If you u can show some message and a link to enable it.

To learn how to make your app location aware, I suggest you follow the official notes here. Thats how I learnt it as well.

wakaka
  • 533
  • 1
  • 7
  • 21