我知道这个问题有许多线索,但没有什么能给我很好的答案。功能onLocationChanged接缝永远不会被调用。
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Toast.makeText(getApplicationContext(), "New Location", Toast.LENGTH_SHORT).show();
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(20));
mMap.addMarker(new MarkerOptions().position(latLng).title("Jesteś tutaj!"));
}
/* ... */
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 2, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3000, 2, locationListener);
}
我做错了什么?
答案 0 :(得分:1)
首先,您需要在真实设备中运行您的应用,而不是虚拟设备,并保持GPS设置。然后你可以使用以下方法为我做这项工作。
private void createMap()
{
TotalDistance.setText("Distance:");
SupportMapFragment supportMapFragment=(SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map);
//SupportMapFragment fm = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
googleMap=supportMapFragment.getMap();
/* googleMap = ((SupportMapFragment) MainActivity.fragmentManager
.findFragmentById(R.id.map)).getMap();*/
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
googleMap.getUiSettings().setRotateGesturesEnabled(false);
marker = new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title(location.getLatitude() + " " + location.getLongitude());
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(location.getLatitude(), location.getLongitude())).zoom(15).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
origin_latLng = new LatLng(location.getLatitude(), location.getLongitude());
}
private void intializeMap(Context context) {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity());
// Showing status
if (status != ConnectionResult.SUCCESS) { // Google Play Services are not available
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, getActivity(), requestCode);
dialog.show();
} else { // Google Play Services are available
// Getting reference to the SupportMapFragment of activity_main.xml
if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
googleMap.setMyLocationEnabled(true);
googleMap.clear();
if (location != null) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 2000, 1, YOUR ACTIVITY REFERENCE);
googleMap.stopAnimation();
}
}
@Override
public void onLocationChanged(Location location) {
// Getting latitude of the current location
double latitude = location.getLatitude();
double longitude = location.getLongitude();
finalLatlang=new LatLng(latitude,longitude);
// userlatLang.add(finalLatlang);
CameraPosition cameraPosition = new CameraPosition.Builder().target(finalLatlang).zoom(15).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Log.d("Latitude", " " + latitude);
Log.d("Longitude", " " + longitude);
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(finalLatlang));
if(flag==1) {
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
flag++;
}
}
另外请将LocationListener实现到Activity或Fragment
中答案 1 :(得分:0)
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
String best = mgr.getBestProvider(crit, false);
以上是获得最佳位置提供商。
您可以在onResume();
中提供您的locationManager请求 @Override public void onResume() {
super.onResume();
locationManager.requestLocationUpdates(, 10000, 1, locationListener);}
希望这可以帮助你。