Search code examples
javaandroidgoogle-mapslocationcoordinates

Android - get coordinates accurately like Google Maps


How can I get my current coordinates accurately like Google Maps?

By now, I have used getLastLocation - it worked, but I get the same coordinates for the same area of 20 meters - it's not that accurate. In Google Maps you can see accuracy of about 3 meters. How can I achive this?

I'm interested only in the coordinates


Solution

  • There are a few things you need to do. Make sure you have permission, make sure user has his GPS is enabled or not. You need to make a location manager and a location listener that updates every time it changes. See below for the different codes.

    Add this to the manifest file to get permission

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    

    Create a location manager

    LocationManager locationManager = (LocationManager)
    getSystemService(Context.LOCATION_SERVICE);
    

    Check if GPS is on

    try {
        gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch(Exception ex) {}
    
    try {
        network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } catch(Exception ex) {}
    if(!gps_enabled && !network_enabled){
    // It's not enabled address the problem here
    }
    

    Create Listener

    LocationListener locationListener = new MyLocationListener();
    locationManager.requestLocationUpdates(
    LocationManager.GPS_PROVIDER, 5000, 10, locationListener);
    

    Listener Class Sample Code

    /*---------- Listener class to get coordinates ------------- */
    private class MyLocationListener implements LocationListener {
    
        @Override
        public void onLocationChanged(Location loc) {
            editLocation.setText("");
            pb.setVisibility(View.INVISIBLE);
            Toast.makeText(
                    getBaseContext(),
                    "Location changed: Lat: " + loc.getLatitude() + " Lng: "
                        + loc.getLongitude(), Toast.LENGTH_SHORT).show();
            String longitude = "Longitude: " + loc.getLongitude();
            Log.v(TAG, longitude);
            String latitude = "Latitude: " + loc.getLatitude();
            Log.v(TAG, latitude);
    
            /*------- To get city name from coordinates -------- */
            String cityName = null;
            Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
            List<Address> addresses;
            try {
                addresses = gcd.getFromLocation(loc.getLatitude(),
                        loc.getLongitude(), 1);
                if (addresses.size() > 0) {
                    System.out.println(addresses.get(0).getLocality());
                    cityName = addresses.get(0).getLocality();
                }
            }
    catch (IOException e) {
                e.printStackTrace();
            }
            String s = longitude + "\n" + latitude + "\n\nMy Current City is: "
                + cityName;
            editLocation.setText(s);
        }
    
        @Override
        public void onProviderDisabled(String provider) {}
    
        @Override
        public void onProviderEnabled(String provider) {}
    
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    }