Lokesh Patel
Lokesh Patel

Reputation: 1162

How to get exact latitude and longitude coordinates on an Android app?

I am developing a map view based app and getting latitude and longitude and show on the map for drawing the route with google api. When I try app on the road (with bike), then get wrong latitude and longitude means get both direction and route like a circle.

I want to exact latitude and longitude coordinates on an Android app.

Bellow code I have used for location.

public Location getLocation() {
    try {
        locationManager = (LocationManager) this.getApplicationContext().getSystemService(LOCATION_SERVICE);
        // getting GPS status
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        // getting network status
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (!isGPSEnabled && !isNetworkEnabled) {
            Toast.makeText(this, "Youe GPS service is not enabled",Toast.LENGTH_SHORT).show();
        } else {
               if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,GPS_TIME_INTERVAL,GPS_DISTANCE, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                 }
              }
          else if (isGPSEnabled) {
                    if (location == null) {
                        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,GPS_TIME_INTERVAL,GPS_DISTANCE, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        } 
                    }
                }
           if(location != null)
            persistLocation(location);
            stopUsingGPS();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return location;
}

Upvotes: 0

Views: 1007

Answers (2)

dileep krishnan
dileep krishnan

Reputation: 344

using Google Play Service Location Api we will get Latitude and Longitude. But you wont get exact Latitude and Longitude , every second it will vary.

Upvotes: 0

carthee
carthee

Reputation: 110

Prefer Google Play Service Location Api over Android framework location Api.

It has more features than the old one.

Upvotes: 1

Related Questions