【问题标题】:how to rotate google map marker to road direction如何将谷歌地图标记旋转到道路方向
【发布时间】:2017-05-30 17:51:22
【问题描述】:

我想将标记图标旋转到道路方向,正如您在图像中看到的那样,它只是放置但没有旋转到道路方向..我已经尝试过下面的代码

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

注意:标记有单独的位置(ofcource)我不想显示从到位置的方位...只有单独的标记要面对道路方向

谢谢你

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

【问题讨论】:

  • 如果你是从api中取回,上传端必须提供方位让服务器返回给你。这是因为您不想根据当前位置设置方位角,而是根据所提供位置的实际方位角设置。

标签: android google-maps google-maps-api-3 google-maps-markers


【解决方案1】:

我已经试过你的代码,首先检查 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);
                            }
                        }
                    }
                });

【讨论】:

  • 不,它不工作:(标记仍然是垂直的(不是按照道路方向)..你能分享整个代码吗?或者你想要我的更多代码供参考
  • @BSavaliya 我使用的相同代码方向没有改变。float bearing = prevLoc.bearingTo(current_location); markerOptions.rotation(bearing) 这样做对我有帮助
【解决方案2】:

以下代码运行良好。

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

【讨论】:

    猜你喜欢
    • 2020-07-24
    • 2012-05-20
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多