0
votes

I can get longitude and latitude and convert them into geopoint when the device get location by network provider, but it fails when I am using gps. What can I do to fix this problem?

public void onCreate(Bundle savedInstanceState) {
...

mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (mLocation != null) {

            gp1 = getGeoByLocation(mLocation);
            gp2 = gp1;

            refreshMapView();

            if( !mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000,
                        10, mLocationListener);
            }else{
                mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000,
                                10, mLocationListener);
            }

    private GeoPoint getGeoByLocation(Location location) {
        GeoPoint gp = null;
        try {
            if (location != null) {
                double geoLatitude = location.getLatitude() * 1E6;
                double geoLongitude = location.getLongitude() * 1E6;
                gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return gp;
    }
2
what's the problem? what do you see in the logs? - Pedro Loureiro
Cannot display the string about longitude and latitude, also the image that point out my location when I am using gps;( - Hello Boy

2 Answers

1
votes

isProviderEnabled will false if it is disabled in the Settings menu. If GPS is enabled you should see an icon on the notification/status bar.

1
votes

GPS is much slower and often not available. If somebody is in a building, such as an office where software is developed or even a house/apartment of a software developer, then there is a good chance that you won't get a GPS fix. Our app relies heavily on location, and the bulk of the location coords we get are either from cell tower or wifi triangulation, both of which show up as NETWORK_PROVIDER.