如何将谷歌地图标记旋转到道路方向

问题描述 投票:1回答:2

我想将标记图标旋转到道路方向,因为你可以在图像中看到它只是刚放置但它没有旋转到道路方向..我已尝试下面的代码到目前为止

double lat = 19.205681
double lon = 72.871742
loc.setLatitude(lat);
loc.setLongitude(lon);
Location newLoc = new Location("service Provider");
newLoc.setLongitude(lat);
newLoc.setLongitude(lon);
map.addMarker(new MarkerOptions()
    .position(new LatLng(data.getLat(),data.getLon()))
    .icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_pandu_car))
    .anchor(0.5f,0.5f)
    .rotation(loc.bearingTo(newLoc))
    .flat(true));

注意:标记有个别位置(当然)我不想显示从到位置承载...只有在道路方向面对的单个标记

谢谢你enter image description here

更新:我想设置我的标记(汽车)像这个奥拉应用程序

enter image description here

android google-maps google-maps-api-3 google-maps-markers
2个回答
0
投票

我已经尝试过你的代码,首先检查一下newLoc是否承载?刚刚进行了一些更改,您可以尝试:

   LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest,
                new com.google.android.gms.location.LocationListener() {
                    @Override
                    public void onLocationChanged(Location location1) {
                        if (location1 != null) {
                            if (currentPositionMarker != null) {
                                currentPositionMarker.remove();
                            }
                            double latitude = location1.getLatitude();
                            double longitude = location1.getLongitude();
                            LatLng latLng = new LatLng(latitude, longitude);
                            int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(activity);
                            if (status == ConnectionResult.SUCCESS) {
                                currentPositionMarker = googleMap.addMarker(new MarkerOptions().position(latLng)
                                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.current_position))
                                        .rotation(location1.getBearing()).flat(true).anchor(0.5f, 0.5f)
                                        .alpha((float) 0.91));

                            } else {
                                GooglePlayServicesUtil.getErrorDialog(status, activity, status);
                            }
                        }
                    }
                });

0
投票

以下代码完美无缺。

    private double radiansToDegrees(double x) {
        return x * 180.0 / Math.PI;
    }


    double fLat = (Math.PI * past.getLatitude()) / 180.0f;
    double fLng = (Math.PI * past.getLongitude()) / 180.0f;
    double tLat = (Math.PI * next.getLatitude()) / 180.0f;
    double tLng = (Math.PI * next.getLongitude()) / 180.0f;

    double degree = radiansToDegrees(Math.atan2(sin(tLng - fLng) * cos(tLat), cos(fLat) * sin(tLat) - sin(fLat) * cos(tLat) * cos(tLng - fLng)));

    if (degree >= 0) {
        bearing = degree;
    } else {
        bearing = 360 + degree;
    }

    marker.rotation((float) bearing);
© www.soinside.com 2019 - 2024. All rights reserved.