在执行围绕用户当前位置标记的地图旋转任务时,有一些奇怪的事情。所以我想做的就是像第二个“当前位置”点击谷歌地图应用程序。当地图移动时,标记必须保持其在地图中心的位置。
据我所知,需要使用方位值来更新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);
每次有错误的hasBearing()调用结果并且使用我的app承载0.0时,每个位置都会返回错误。但谷歌地图应用程序正好向我展示了我的方向。
我正在使用服务
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);
}`
有人知道谷歌地图如何为自动定位地图模式进行轴承计算? 也许某人有这样的情况,可以帮助我提出建议。感谢。
答案 0 :(得分:1)
这可以帮到你
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));
}
}