在执行用户当前位置标记的地图旋转任务时,这里有一些奇怪的事情。因此,我想做的就是像Google Maps应用程序上的第二个"当前位置"点击。在地图移动时,标记必须将其位置保持在地图中心。
我知道需要使用轴承值来更新googlemap v2上的相机位置对象。
CameraUpdate cameraUpdatePos;
CameraPosition.Builder currentPlaceBuilder = new CameraPosition.Builder().target(loc);
if (location.hasBearing())
currentPlaceBuilder.bearing(location.getBearing());
cameraUpdatePos = CameraUpdateFactory.newCameraPosition(currentPlaceBuilder.build());
map.animateCamera(cameraUpdatePos);
这个错误是每个位置每次false hasbeartiar((呼叫结果都会返回,并且在我的应用程序上以0.0的速度承受0.0。但是Google Maps应用程序目前正确显示了我的方向。
我正在使用服务
LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
googleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(gmsCallbacks)
.addOnConnectionFailedListener(gmsCallbacks)
.addApi(LocationServices.API)
.build();
locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(25 * 1000)
.setFastestInterval(5 * 1000)
.setSmallestDisplacement(1);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest)
.setAlwaysShow(true);`
和方法连接
`@Override
public void onConnected(@Nullable Bundle bundle) {
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
return;
Log.i(TAG, "Connected");
location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, gmsCallbacks);
if (locationChangeCallback != null)
locationChangeCallback.onLocationChanged(location);
}`
有人知道Google Map如何对自动方向地图模式进行轴承计算?也许有人有这样的情况,可以帮助我提供建议。谢谢。
可以帮助您
public class GoogleApiActivity extends AppCompatActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener , {
SupportMapFragment fragment;
GoogleMap googleMap;
GoogleApiClient googleApiClient;
Marker marker;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_google_api);
mapLoading();
googleClientApi();
}
private void mapLoading(){
FragmentManager manager = getSupportFragmentManager();
fragment = (SupportMapFragment) manager.findFragmentById(R.id.map_space);
if (fragment == null)
{
fragment = SupportMapFragment.newInstance();
manager.beginTransaction().replace(R.id.map_space, fragment).commit();
}
}
@Override
protected void onResume(){
super.onResume();
if (googleMap == null){
fragment.getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap googleMap){
this.googleMap = googleMap;
Toast.makeText(this, "Map Ready CallBack", Toast.LENGTH_SHORT).show();
this.googleMap.setTrafficEnabled(true);
this.googleMap.setIndoorEnabled(true);
this.googleMap.setBuildingsEnabled(true);
this.googleMap.getUiSettings().setZoomControlsEnabled(true);
this.googleMap.setOnCameraChangeListener(this);
}
private void googleClientApi(){
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
public void onConnected(@Nullable Bundle bundle){
LocationRequest locationRequest = createLocationRequest();
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.
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, createLocationRequest(),GoogleApiActivity.this);
}
@Override
public void onConnectionSuspended(int i){
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult){
}
public LocationRequest createLocationRequest()
{
LocationRequest locationRequest = new LocationRequest();
locationRequest.setInterval(0);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setSmallestDisplacement(0);
return locationRequest;
}
@Override
protected void onStart()
{
super.onStart();
if (googleApiClient != null)
{
googleApiClient.connect();
}
}
@Override
protected void onStop()
{
super.onStop();
if (googleApiClient != null)
{
googleApiClient.disconnect();
}
}
@Override
public void onLocationChanged(Location location){
Toast.makeText(this, "Inside onLocationChanged "+location.getAccuracy(), Toast.LENGTH_SHORT).show();
if (location != null)
{
if (marker == null){
marker = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(location.getLatitude(), location.getLongitude()))
.title("My Location"));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 18));
}else{
marker.setPosition(new LatLng(location.getLatitude(),location.getLongitude()));
updateCameraBearing(googleMap, location.getBearing(),location);
}
} }
private void updateCameraBearing(GoogleMap googleMap, float bearing, Location location) {
if ( googleMap == null) return;
CameraPosition camPos = CameraPosition
.builder(
googleMap.getCameraPosition() // current Camera
).target(new LatLng(location.getLatitude(),location.getLongitude()))
.bearing(bearing)
.build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(camPos));
}
}