0

i tried a lot of "get current location" for android. But i think most of them are outdated, or i simply dont get it.

I DONT want to set a marker.addmarker(params..) i would like to use the blue default dot for my position on Gmaps. Here i've found somthing about that, but for my bad it also aint work.

Customize marker of myLocation Google Maps v2 Android

So in first line i need, my current Location with a Listener when my Location is updating. Im trying this right now (Testing on a real device). But myLocation is always =null.

    //get GMaps Fragment
    mMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
    mMap.setMyLocationEnabled(true);    //activate Blue dot

    myLocation = mMap.getMyLocation();  //Testing, but not working = null

    //Trying with LocationManager
    locManager =(LocationManager)getSystemService(this.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locManager.getBestProvider(criteria, true);

    myLocation = locManager.getLastKnownLocation(provider);
    if(myLocation != null){
        double lat = myLocation.getLatitude();
        double lon = myLocation.getLongitude();
    }

    locManager.requestLocationUpdates(provider, 1000, 0, new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            myLocation = location;
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    });

2 Answers 2

0

Follow this http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/ . Then just place the latitude and longitude you get(using gps.getLatitude(), gps.getLongitude()) in your google map and it will show your current location.

5
  • Thats a lot of code for just retrieving my location but i will have a try. Thank you!
    – Niko
    Apr 8, 2014 at 8:14
  • If you are gonna use GPS for location retrieval make sure you are in an open place. GPS sometimes doesn't work when you have your device inside a building Apr 8, 2014 at 8:15
  • Using the above code link that I have provided,you can find the location both by using networks as well as gps.
    – kgandroid
    Apr 8, 2014 at 8:21
  • Thanks to kgandroid. Helped me to get my location finally. But i have to write this class on my own, to get a onLocationChanged Listner into it
    – Niko
    Apr 8, 2014 at 9:06
  • Unfortunalty i cant awnser my own question so i write it down here, with this code you get the location from google and not from your mobile phone: mMap.setOnMyLocationListener(new...) in this Listener you can call mMap.getMyLocation.
    – Niko
    Apr 8, 2014 at 9:37
0

Try this code, it is worked in my application:

private LocationManager locationManager;
private String provider;
double lat,lon;
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.locate);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 1, this);
        if ( !locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
            gpsalert();
        }
        // Criteria to select location provider

        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if (location != null) {

            Toast.makeText(getApplicationContext(), "Provider is available", Toast.LENGTH_SHORT).show();

        } else {
            Toast.makeText(getApplicationContext(), "Provider is not available", Toast.LENGTH_SHORT).show();
        }
    }
@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

    String addr,city,country;
    lat=location.getLatitude();
    lon=location.getLongitude();

    //Toast.makeText(getApplicationContext(), "lat"+lat+"long"+lon, Toast.LENGTH_LONG).show();
    Geocoder geocoder;
    List<Address> addresses;
    geocoder = new Geocoder(this, Locale.getDefault());
    try {
        addresses = geocoder.getFromLocation(lat, lon, 1);

         addr = addresses.get(0).getAddressLine(0);
        city = addresses.get(0).getAddressLine(1);
         country = addresses.get(0).getAddressLine(2);
         t1.setText(addr+"\n"+city+"\n"+country);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub
    //Toast.makeText(this, "Disabled provider " + provider, Toast.LENGTH_SHORT).show();
    //t1.setText("");
}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub
    //Toast.makeText(this, "Enabled provider " + provider, Toast.LENGTH_SHORT).show();
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}
@Override
protected void onResume() {
    super.onResume();
    //locationManager.requestLocationUpdates(provider, 400, 1, this);
}
                                    // Remove LocationListener updates
@Override
protected void onPause() {
    super.onPause();
    //locationManager.removeUpdates(this);
}
public void gpsalert()
{
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
           .setCancelable(false)
           .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
               public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                   startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
               }
           })
           .setNegativeButton("No", new DialogInterface.OnClickListener() {
               public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                    dialog.cancel();
               }
           });
    final AlertDialog alert = builder.create();
    alert.show();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.