Get Current Location GMaps

后端 未结 2 442
挽巷
挽巷 2021-01-27 19:46

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

相关标签:
2条回答
  • 2021-01-27 19:57

    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();
    }
    
    0 讨论(0)
  • 2021-01-27 20:07

    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.

    0 讨论(0)
提交回复
热议问题