Muhammad Jamal
Muhammad Jamal

Reputation: 442

Unable to get accurate location from GPS

I am unable to get accurate Latitude , longitude from GPS .. I need to get The location of the small blue pointer(My Current Location) on Google maps in Android ... but I am getting the Location where Red marker is placed manually. Here is the ScreenSnap.. enter image description here Here is the code for getting the location of red marker.

                if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
                if(isGPSEnabled) {
                if(location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                    if(locationManager != null) {
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                        if(location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }

Upvotes: 1

Views: 680

Answers (1)

Android Enthusiast
Android Enthusiast

Reputation: 4960

Use 'FusedLocationApi.requestLocationUpdates' to get current location. The fused location provider is one of the location APIs in Google Play Services. It manages the underlying location technology and provides a simple API so that you can specify requirements at high level. Also, don't forget to enable device GPS.

Here's a sample demo app for FusedLocationApi, it shows how to use the said API: https://github.com/codepath/android-google-maps-demo/blob/master/app/src/main/java/com/example/mapdemo/MapDemoActivity.java

Upvotes: 0

Related Questions