0

Android Marshmallow API 23で緯度と経度の値が0.0になるという問題が発生します。その他のAPIは正常に動作しています。現在の位置情報を取得していないGoogleマップに問題があります。Android Marshmallow API 23で緯度と経度が表示されない

public Location getLocation() { 
      try { 
       locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE); 
       // getting GPS status 
       isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
       // getting network status 
       isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
       if (!isGPSEnabled && !isNetworkEnabled) { 
        // no network provider is enabled 
       } else { 
        this.canGetLocation = true; 
        // First get location from Network Provider 
        if (isNetworkEnabled) { 
         locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
         Log.d("Network", "Network"); 
         if (locationManager != null) { 
          location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
          if (location != null) { 
           latitude = location.getLatitude(); 
           longitude = location.getLongitude(); 
          } 
         } 
        } 
        // if GPS Enabled get lat/long using GPS Services 
        if (isGPSEnabled) { 
         if (location == null) { 
          locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
          Log.d("GPS Enabled", "GPS Enabled"); 
          if (locationManager != null) { 
           location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
           if (location != null) { 
            latitude = location.getLatitude(); 
            longitude = location.getLongitude(); 
           } 
          } 
         } 
        } 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return location; 
     } 
public void stopUsingGPS() { 
     if (locationManager != null) { 
      locationManager.removeUpdates(GPSTracker.this); 
     } 
    } 
    public double getLatitude() { 
     if (location != null) { 
      latitude = location.getLatitude(); 
     } 
     // return latitude 
     return latitude; 
    } 
    public double getLongitude() { 
     if (location != null) { 
      longitude = location.getLongitude(); 
     } 
     // return longitude 
     return longitude; 
    } 
    /** 
    * Function to check GPS/wifi enabled 
    * 
    * @return boolean 
    * */ 
    public boolean canGetLocation() { 
     return this.canGetLocation; 
    } 

    /** 
    * Function to show settings alert dialog On pressing Settings button will 
    * lauch Settings Options 
    * */ 
    public void showSettingsAlert() { 
     final Dialog dialog = new Dialog(mContext,android.R.style.Theme_Translucent_NoTitleBar); 
     dialog.setContentView(R.layout.upgrade_now_layout); 
     LinearLayout upgradeParent = (LinearLayout) dialog 
       .findViewById(R.id.upgradeParent); 
     LinearLayout laterParent = (LinearLayout) dialog.findViewById(R.id.laterParent); 
     TextView title = (TextView) dialog.findViewById(R.id.title); 
     TextView discription = (TextView) dialog.findViewById(R.id.discription); 
     TextView yes = (TextView) dialog.findViewById(R.id.yes); 
     TextView no = (TextView) dialog.findViewById(R.id.no); 
     title.setVisibility(View.GONE); 
     discription.setText(" GPS is not enabled. Do you want to go to settings menu? \n"); 
     yes.setText("Setting"); 
     no.setText("Cancel"); 
     laterParent.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       Splash.again = true; 
       dialog.dismiss(); 
      } 
     }); 
     upgradeParent.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // super.onBackPressed(); 
       Intent intent = new Intent(
         Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       mContext.startActivity(intent); 
       Splash.again = true; 
       dialog.dismiss(); 

      } 
     }); 
     dialog.show(); 
    } 

アンドロイドたManifest.xml

<uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission-sdk-23 android:name="android.permission.ACCESS_FINE_LOCATION"/> 

答えて

1

あなたが手動で設定

から許可を有効にするために持っているよりも、これを実装していない場合は、マシュマロ Hereで、実行時のアクセス許可を実装しており、

Googleコンソールアカウント

から取得する AndroidManifest.xmlのAPIキーを確認してください
<meta-data 
     android:name="com.google.android.maps.v2.API_KEY" 
     android:value="YOUR_KEY" /> 
+0

<メタデータ アンドロイド:名= "com.google.android.maps.v2.API_KEY" アンドロイド:値= "YOUR_KEY" />はい私はこれを使用するが、 –

+0

を得ていない、あなたを持っています許可の設定を有効にしますか? –

+0

yes.myコードはAndroid版で動作しています。私はこのエラー23 APIを取得しています。 –

0
Implement run time permission for mashmallow 
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     checkLocationPermission(); 
    } else { 
     Log.d("onCreate", "Google Play Services available."); 
    } 
public boolean checkLocationPermission() { 
     if (ContextCompat.checkSelfPermission(getActivity(), 
       Manifest.permission.ACCESS_FINE_LOCATION) 
       != PackageManager.PERMISSION_GRANTED) { 

      // Asking user if explanation is needed 
      if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), 
        Manifest.permission.ACCESS_FINE_LOCATION)) { 

       // Show an explanation to the user *asynchronously* -- don't block 
       // this thread waiting for the user's response! After the user 
       // sees the explanation, try again to request the permission. 

       //Prompt the user once explanation has been shown 
       new AlertDialog.Builder(getActivity()) 
         .setTitle("Permission needed") 
         .setMessage("Please allow permission to access your location to continue service") 
         .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialogInterface, int i) { 
           //Prompt the user once explanation has been shown 
           ActivityCompat.requestPermissions(getActivity(), 
             new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
             MY_PERMISSIONS_REQUEST_LOCATION); 
          } 
         }) 
         .create() 
         .show(); 

      } else { 
       // No explanation needed, we can request the permission. 
       ActivityCompat.requestPermissions(getActivity(), 
         new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
         MY_PERMISSIONS_REQUEST_LOCATION); 
      } 
      return false; 
     } else { 
      return true; 
     } 
    } 
関連する問題