2017-04-11 46 views
0

如何在啓用GPS設置後刷新地圖片段? 我使用下面的代碼來啓用GPS設置。啓用GPS設置不起作用後刷新地圖以顯示當前位置

//ask for runtime permission to showing map 

private void askForPermission(String permission, Integer requestCode) { 
    if (ContextCompat.checkSelfPermission(getActivity(), permission) != PackageManager.PERMISSION_GRANTED) { 

     // Should we show an explanation? 
     if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), permission)) { 
      ActivityCompat.requestPermissions(getActivity(), new String[]{permission}, requestCode); 
      //getActivity().finish(); 
     } else { 
      ActivityCompat.requestPermissions(getActivity(), new String[]{permission}, requestCode); 
     } 
    } 
} 

//check the gps is enable or not 
public void statusCheck() { 
    manager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); 

    if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
     buildAlertMessageNoGps(); 
    } 
} 

//alert dialog to enable location permission 
private void buildAlertMessageNoGps() { 
    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    builder.setMessage("Your GPS seems to be disabled, do you want to enable it?") 
      .setCancelable(false) 
      .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
       public void onClick(final DialogInterface dialog, final int id) { 
        startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 
       } 
      }) 
      .setNegativeButton("No", new DialogInterface.OnClickListener() { 
       public void onClick(final DialogInterface dialog, final int id) { 
        dialog.cancel(); 

       } 
      }); 
    final AlertDialog alert = builder.create(); 
    alert.show(); 
} 

回答

0

刷新地圖。創建一個方法,如

private void refreshMap(){ 
if(mMap!=null){ 
      mMap.clear(); 
     } 
} 
0

您必須爲此註冊廣播接收器。檢查文檔中的gps廣播。如果有gps許可或不許可,你可以做兩次。如果沒有權限,你必須在onRequestPrrmissionResult中檢查requestpermission方法的結果,然後在那裏註冊接收者。

P. S.不要忘記從廣播註銷。