简体   繁体   中英

Android Google Maps Api v2 current Location Null

Good Day.Im trying to get current location with map.getMyLocation() but surely I'm getting NullPointerException as my location isn't loaded for a while.So I'm doing a MapReady check and MapLoaded check which both triggers once map ready and its loaded,but unfortunately what about my current location,it seems that i will never know when its available.Here is a snippet of my code which i though might do normal null check.

            @Override
                    public void onMapLoaded() {
        //here my map is loaded so this triggers.
    if(map.getMyLocation()!=null){
//do something here. 

    }
        }

but it seems that my null check will never work as just because once my above written method triggers it dont wait for my if condition its just checks if it is not satisfying my if condition just going over it so my if condition never triggers.Problem is that how can i be notified when my location is enabled?I tried even to put this into loop of while() but its freezes if i do on UI thread and won't let me do anything if i do on a THREAD as google maps need to be done on MainThread .So my question is next,how in world i can be notified whether my location is null,or not?And how can i trigger some piece of code once its not NULL ?

I use this code in my application..
Try Implement this in you app

//Global Declaration
GoogleMap googleMap;
LocationManager locationManager;
Location location;
CameraUpdate cameraUpdate;

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

    location = googleMap.getMyLocation();
    if (location != null) {
        LatLng latLang = new LatLng(location.getLatitude(), location.getLongitude());
        cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLang, 17);
        googleMap.animateCamera(cameraUpdate);
    } else {

        AlertDialog.Builder builder = new AlertDialog.Builder(MapScreen.this);
        builder.setTitle("Warning");
        builder.setMessage("Location Not Found...!!!");
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        }).show();
    }
} else {
    showAlertDialog();
}

private void showAlertDialog () {

    AlertDialog.Builder builder = new AlertDialog.Builder(MapScreen.this);
    builder.setTitle("Warning");
    builder.setMessage("Device GPS is currently OFF. Please Turn ON.");
    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    }).show();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM