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

时间:2017-05-30 17:51:23

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

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

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)我不想显示从到位置承载...只有单个标记在路面方向面对

谢谢你 enter image description here

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

enter image description here

2 个答案:

答案 0 :(得分: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);
                            }
                        }
                    }
                });

答案 1 :(得分: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);