0

i want when the location services disables or changed from GPS to wireless or wireless to GPS the map is displayed . i try more but the app is crashed.

Any help would be greatly appreciated . Thank you in advance.

the java code

if (servicesOK()) {
        setContentView(R.layout.map);
        if (initMap()) {

            Toast.makeText(this, "Ready to map", Toast.LENGTH_SHORT).show();

            gotoLocation();
        } else {
            Toast.makeText(this, "Map not available", Toast.LENGTH_SHORT)
                    .show();

        }
    } else {
        setContentView(R.layout.activity_error);
    }

}



public boolean servicesOK() {
    int isAvailable = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(this);

    if (isAvailable == ConnectionResult.SUCCESS) {
        return true;
    } else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable,
                this, GPS_ERRORDIALOG_REQUEST);
        dialog.show();
    } else {
        Toast.makeText(this, "can't connect to Google Play Services",
                Toast.LENGTH_SHORT).show();
    }
    return false;
}

private boolean initMap() {
    if (Gmap == null) {
        SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        Gmap = mapFrag.getMap();
    }
    return (Gmap != null);
}

private void gotoLocation() {
    // Enable my location layer of Google map

    Gmap.setMyLocationEnabled(true);
    // get LocationManger object from system service LOCATION_SERVICE
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    // create criteria object to retrieve provider
    Criteria criteria = new Criteria();

    // Get the name of best provider
    String provider = locationManager.getBestProvider(criteria, true);
    // Get Current Location 

    Location myLocation = locationManager.getLastKnownLocation(provider);

    // set map type
    Gmap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

    // Get latitude of the current location

    double latitude = myLocation.getLatitude();

    // Get longitude of the current location

    double longitude = myLocation.getLongitude();

    // create latlng object for the current location

    LatLng latlng = new LatLng(latitude, longitude);

    }
Ayman Emad
  • 113
  • 3
  • 12
  • I'm currently having the same problem, have you been able to solve this? Kindly update us. – Emzor Jan 16 '16 at 01:29

1 Answers1

0

with this method you can check what kind of location is enabled:

 public static String checkGPS(Context context) {

        LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        String locationProviders = Settings.Secure.getString(context.getContentResolver(),
                Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            return "Not enable";
        }
        // if gps is enabled and network location is disabled
        // note, GPS can be slow to obtain location data but is more accurate
        else if (locationProviders.contains("gps") && !locationProviders.contains("network")) {
            return "Enable";
        } else if (locationProviders.contains("gps") && locationProviders.contains("network")) {
            return "Enable + Network";
        } else if (!locationProviders.contains("gps") && locationProviders.contains("network")) {
            return "Only Network";
        } else {
            return "Not enable";
        }

    }

Hope it helps you.

  • i add your method in my code and when open wireless is working well but when open gps( note gps not worked in currently palce) doen't work , i want when all services are disabled the map is displayed only. – Ayman Emad Jul 07 '14 at 15:19
  • Then, you need implement a receiver to notify when change the status of GPS. This helps you: http://stackoverflow.com/questions/12665558/gps-status-enabled-disabled-broadcast-receiver – Alejandro Martínez Martínez Jul 07 '14 at 15:24
  • what about this public void onProviderDisabled(String provider) { } public void onProviderEnabled(String provider) { } public void onStatusChanged(String provider, int status, Bundle extras) { } } – Ayman Emad Jul 07 '14 at 15:58