Current Location failed GoogleMap

前端 未结 3 2013
醉梦人生
醉梦人生 2021-01-27 05:36

I want to check if the GPS is on if it is should show the current location. If not it should ask to turn it on. If user click cancel or dont turn the coordinates will be set as

3条回答
  •  终归单人心
    2021-01-27 06:24

    The following code checks, if location is enabled or not. If not it shows alert dialog to enable location service.

    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    try {
        gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }catch (Exception ex){}
    try{
        network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    }catch (Exception ex){}
    if(!gps_enabled && !network_enabled){
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setMessage(getResources().getString(R.string.gps_network_not_enabled));
        dialog.setPositiveButton(getResources().getString(R.string.open_location_settings), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface paramDialogInterface, int paramInt) {                 
                Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                Startup.this.startActivity(myIntent);                    
            }
        });
        dialog.setNegativeButton(getString(R.string.Cancel), new DialogInterface.OnClickListener() {
    
            @Override
            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                // TODO Auto-generated method stub
    
            }
        });
        dialog.show();
    }
    

    add below code in AndroidManifest.xml

    
    
    
    

提交回复
热议问题