【问题标题】:Bearing and location轴承和位置
【发布时间】:2017-11-06 18:14:40
【问题描述】:

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

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

和方法连接

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

有人知道谷歌地图如何为自动定位地图模式进行方位计算吗? 也许有人有这种情况,可以帮助我提供建议。谢谢。

【问题讨论】:

    标签: android google-maps bearing


    【解决方案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));
    }
    

    }

    【讨论】:

    • 非常感谢您的回答,但我已经尝试过了。问题是轴承值总是为零值。我意识到谷歌地图出于某种原因使用指南针传感器。我相信谷歌应用程序正在使用这些传感器,如加速度计和磁力计。但是当我使用这样的传感器时,我不会得到相同的行为。我得到的只是烦人的地图和高负载的 cpu。
    猜你喜欢
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 2014-05-13
    • 2019-05-02
    • 2016-04-21
    • 1970-01-01
    • 2021-04-07
    • 1970-01-01
    相关资源
    最近更新 更多