2017-06-05 70 views
0

在执行围绕用户当前位置标记的地图旋转任务时,在这里得到了一些奇怪的东西。所以我想要做的就是像第二个“当前位置”点击谷歌地图应用程序。标记必须在地图移动时保持其在地图中心的位置。方位和位置

据我所知,需要使用方位值更新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); 

bug是关于每个位置返回每一次假hasBearing()调用结果和轴承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);` 

和方法onconnected

`@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); 
     }` 

有谁知道谷歌地图如何使自动定位地图模式的轴承计算? 也许有人有这样的情况,并可以帮助我的建议。谢谢。

回答

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)); 
} 

}

+0

非常感谢您的回答,但我已经尝试过这一点。问题是方位值总是零值。我意识到谷歌地图使用指南针传感器出于某种原因。我相信谷歌应用程序正在使用这些传感器,如加速计和磁性。但是当我使用这样的传感器时,我不会得到相同的行为。我得到的是ntsic地图和高负载的CPU。 – Magirus