我想取回手机的位置,但我对这一行有疑问:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,10000,0,(LocationListener) this);
我知道问题是权限检查,但我不知道在我的代码中执行此操作:
package com.cmn.cmnvtc;
import android.app.Fragment;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainPageFragment1 extends Fragment implements OnMapReadyCallback, LocationListener {
LocationManager locationManager;
public LatLng myLocation;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_main1, container, false);
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,10000,0,(LocationListener) this);
return v;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MapFragment fragment = (MapFragment) getChildFragmentManager().findFragmentById(R.id.map);
fragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
LatLng marker = new LatLng(48.9818555, 2.270541);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 15));
googleMap.addMarker(new MarkerOptions().title("Le garage").position(marker));
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
@Override
public void onLocationChanged(Location location) {
if(location != null){
Toast.makeText(getActivity(), "Latitude:" + location.getLatitude() + " - Longitude:" + location.getLongitude(), Toast.LENGTH_SHORT);
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
我该怎么办呢?
我的错误在于代码:"添加权限检查"
答案 0 :(得分:1)
您需要检查 Marshmollow
private static final int GRANTED = PackageManager.PERMISSION_GRANTED;
if (checkCoarseLocationPermission(getActivity()) && checkFineLocationPermission(getActivity())) {
//permissions are granted
}else {
requestLocationPermission(getActivity());
}
}
public boolean checkCoarseLocationPermission(Activity mActivity){
int permissionCheck = ContextCompat.checkSelfPermission(mActivity,
Manifest.permission.ACCESS_COARSE_LOCATION);
return permissionCheck == GRANTED;
}
public boolean checkFineLocationPermission(Activity mActivity){
int permissionCheck = ContextCompat.checkSelfPermission(mActivity,
Manifest.permission.ACCESS_FINE_LOCATION);
return permissionCheck == GRANTED;
}
public void requestLocationPermission(Activity mActivity){
ActivityCompat.requestPermissions(mActivity,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
}
答案 1 :(得分:0)
试试这个 public void onLocationChanged(Location location){ if(location!= null){ location = locationManager .getLastKnownLocation(LocationManager.GPS_PROVIDER); Toast.makeText(getActivity(),“Latitude:”+ location.getLatitude()+“ - Longitude:”+ location.getLongitude(),Toast.LENGTH_SHORT); } }
答案 2 :(得分:0)
您需要在应用清单中添加权限。打开manifest.xml并将这两行放在:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
ACCESS_FINE_LOCATION允许您的应用使用实际GPS跟踪用户位置。 ACCESS_COARSE_LOCATION允许您的应用使用WiFi网络跟踪用户位置,因此如果您只使用GPS,则可以省略此位置。