Get Current Location GMaps

谁说我不能喝 提交于 2019-12-02 11:54:34

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.

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();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!