1
我想在地圖上獲取當前位置並從當前路徑繪製到未知點。繪製路徑與步行。我已經集成了谷歌地圖,但我不知道如何獲取當前位置。我試過這個但沒有找到位置。在地圖上獲取當前位置
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
public void onMapReady(GoogleMap mMap) {
googleMap=mMap;
if (googleMap != null) {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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.
try {
if (PreferenceClass.getStringPreferences(mContext, Constant.CURRENT_LATITUDE).trim().length() > 0) {
LatLng latLng = new LatLng(Double.parseDouble(PreferenceClass.getStringPreferences(mContext, Constant.CURRENT_LATITUDE)), Double.parseDouble(PreferenceClass.getStringPreferences(mContext, Constant.CURRENT_LONGITUDE)));
Log.d("latLng", latLng.toString());
googleMap.addMarker(new MarkerOptions().position(latLng).title("My Location").icon(BitmapDescriptorFactory.fromResource(R.drawable.profile)));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
googleMap.setMyLocationEnabled(true);
}
} catch (Exception e) {
e.printStackTrace();
}
return;
}
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setRotateGesturesEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.getUiSettings().setZoomGesturesEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, this);
}
}
public void onLocationChanged(Location location) {
googleMap.clear();
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("My Location");
googleMap.addMarker(markerOptions);
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17.0f));
}
這是獲取當前位置,但如何行走時從當前位置上繪製路線。 – user7108398