gpt4 book ai didi

android - 在 Google map 上移动并显示驾驶摄像头 View

转载 作者:行者123 更新时间:2023-12-02 10:35:11 25 4
gpt4 key购买 nike

我已通过谷歌地图路由添加了当前位置

        Routing routing = new Routing.Builder()
.travelMode(Routing.TravelMode.DRIVING)
.key(getResources().getString(R.string.google_maps_api))
.withListener(this)
.waypoints(new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude()), site_location)
.alternativeRoutes(false)
.build();
routing.execute();



@Override
public void onRoutingSuccess(ArrayList<Route> route, int shortestRouteIndex) {

if (polylines.size() > 0) {
for (Polyline poly : polylines) {
poly.remove();
}
}

polylines = new ArrayList<>();
//add route(s) to the map.
for (int i = 0; i < route.size(); i++) {

//In case of more than 5 alternative routes
int colorIndex = i % COLORS.length;

PolylineOptions polyOptions = new PolylineOptions();
polyOptions.color(getResources().getColor(COLORS[colorIndex]));
polyOptions.width(10 + i * 13);
polyOptions.addAll(route.get(i).getPoints());
Polyline polyline = googleMap.addPolyline(polyOptions);
polylines.add(polyline);

int distance = route.get(i).getDistanceValue();
if (distance < 1000){
totalKm.setText( distance+" Metres");
}else {
totalKm.setText( (distance/1000) +" km");

}
}

LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude()));
builder.include(site_marker.getPosition());
LatLngBounds bounds = builder.build();
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 100);
googleMap.animateCamera(cu);

}

这会显示行车路线,例如

enter image description here

但我试图显示默认的谷歌地图驾驶图标与缩放 View ,如

enter image description here

如何继续添加这样的 map ,同时仍然保留折线以显示驾驶 View 。

最佳答案

Jinesh Francis他的回答完全正确:您应该通过 Intent 运行默认 map Google map 应用程序,或者修改标准 MapView (或 MapFragment)。

TLDR;

如果您选择第二种方法 - 最简单的方法是使用 Android Google Maps API 的标准类来创建 View ,如您的示例所示(其他方法是创建 MapView-based custom view )。

首先请仔细阅读Google Maps Platform Terms of Service第3.2.4条禁止滥用服务(d) :

(d) No Re-Creating Google Products or Features. Customer will not use the Services to create a product or service with features that are substantially similar to or that re-create the features of another Google product or service. Customer’s product or service must contain substantial, independent value and features beyond the Google products or services. For example, Customer will not: (i) re-distribute the Google Maps Core Services or pass them off as if they were Customer’s services; (ii) create a substitute of the Google Maps Core Services, Google Maps, or Google Maps mobile apps, or their features; (iii) use the Google Maps Core Services in a listings or directory service or to create or augment an advertising product; (iv) combine data from the Directions API, Geolocation API, and Maps SDK for Android to create real-time navigation functionality substantially similar to the functionality provided by the Google Maps for Android mobile app.

如果您不违反服务条款,您可以通过这些步骤/任务执行您想要的操作:

1)获取用户当前位置;

2)获取距离用户当前位置最近的路线路径段(因为用户位置很少恰好在道路上);

3)获取该段的方位角(方位角);

4) 根据路径段方位以适当的倾斜和旋转显示带有路线路径和用户当前位置标记的 map 。

任务 1 可以像 this 那样解决Axxiss的回答:

private final LocationListener mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(final Location location) {
//your code here
}
};

任务 2 可以通过 PolyUtil.isLocationOnPath() 解决就像 that answer :

private LatLng getMarkerProjectionOnSegment(LatLng carPos, List<LatLng> segment, Projection projection) {
LatLng markerProjection = null;

Point carPosOnScreen = projection.toScreenLocation(carPos);
Point p1 = projection.toScreenLocation(segment.get(0));
Point p2 = projection.toScreenLocation(segment.get(1));
Point carPosOnSegment = new Point();

float denominator = (p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y);
// p1 and p2 are the same
if (Math.abs(denominator) <= 1E-10) {
markerProjection = segment.get(0);
} else {
float t = (carPosOnScreen.x * (p2.x - p1.x) - (p2.x - p1.x) * p1.x
+ carPosOnScreen.y * (p2.y - p1.y) - (p2.y - p1.y) * p1.y) / denominator;
carPosOnSegment.x = (int) (p1.x + (p2.x - p1.x) * t);
carPosOnSegment.y = (int) (p1.y + (p2.y - p1.y) * t);
markerProjection = projection.fromScreenLocation(carPosOnSegment);
}
return markerProjection;
}

任务 3 可以用这样的代码来解决:

private float getBearing(LatLng begin, LatLng end) {
double dLon = (end.longitude - begin.longitude);
double x = Math.sin(Math.toRadians(dLon)) * Math.cos(Math.toRadians(end.latitude));
double y = Math.cos(Math.toRadians(begin.latitude))*Math.sin(Math.toRadians(end.latitude))
- Math.sin(Math.toRadians(begin.latitude))*Math.cos(Math.toRadians(end.latitude)) * Math.cos(Math.toRadians(dLon));
double bearing = Math.toDegrees((Math.atan2(x, y)));
return (float) bearing;
}

其中beginend是当前路由路径段的开始和结束。

任务 4 可以用这样的代码来解决:

作为标记,您可以使用向北箭头的矢量绘制,如下所示:

User location marker

ic_up_arrow_circle.xml(您也可以调整透明度和颜色):

<vector android:height="24dp" android:viewportHeight="93.934"
android:viewportWidth="93.934"
android:width="24dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<path
android:fillColor="#8fFF0000"
android:pathData="m0,46.9666c0,25.939 21.028,46.967 46.967,46.967c25.939,-0 46.967,-21.028 46.967,-46.967c0,-25.939 -21.027,-46.967 -46.967,-46.967c-25.939,-0 -46.967,21.028 -46.967,46.967zM78.262,67.4396l-31.295,-16.845l-31.295,16.845l31.295,-51.614l31.295,51.614z"
/>

<path
android:fillColor="#FFFFFF"
android:pathData="M78.262,67.4396l-31.295,-16.845l-31.295,16.845l31.295,-51.614l31.295,51.614z"
/>
</vector>

您可以使用如下代码将其放置在 map 上:

public Marker addDirectionMarker(LatLng latLng, float angle) {
Drawable circleDrawable = ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_up_arrow_in_circle);
BitmapDescriptor markerIcon = getMarkerIconFromDrawable(circleDrawable, 150, 150);

return mGoogleMap.addMarker(new MarkerOptions()
.position(latLng)
.anchor(0.5f, 0.5f)
.rotation(angle)
.flat(true)
.icon(markerIcon)
);
}

其中 150 是以像素为单位的标记大小。注意!您需要 flat 标记来随 map 旋转和倾斜,并需要 0.5f 来将标记 anchor 精确移动到其中心点。

然后您可以在 map 上显示所有这些:

...
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(userPosition)
.tilt(tilt)
.zoom(zoom)
.bearing(bearing)
.build();
mGoogleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
...

但是,如果您只执行用户当前位置标记出现在屏幕中心的操作(因为 GoogleMap.moveCamera() 将中心精确设置在 .target() )。因此,为了避免这种情况,您需要稍微向下移动 map - 在这种情况下,用户位置标记应该出现在屏幕底部。对于 map 中心移动,您需要获取当前 map 中心屏幕坐标,然后更改 y 坐标并获取新的屏幕中心。类似这样的事情:

...
LatLng mapCenter = mGoogleMap.getCameraPosition().target;
Projection projection = mGoogleMap.getProjection();
Point centerPoint = projection.toScreenLocation(mapCenter);

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int displayHeight = displayMetrics.heightPixels;

centerPoint.y = centerPoint.y - (int) (displayHeight / 4.5); // move center down for approx 22%

LatLng newCenterPoint = projection.fromScreenLocation(centerPoint);

mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(newCenterPoint, zoom));
...

有了所有这些东西,对于您的路线(使用 zoom = 15 和 tilt = 50),您应该得到类似的东西:

Directions view

正如您所看到的,路线路径并不完全在道路上,因此您需要比 Directions API 响应更精确地获取路线路径点。你可以得到这个,例如通过 Google Maps Roads API部分Snap to Road其中

returns the best-fit road geometry for a given set of GPS coordinates. This service takes up to 100 GPS points collected along a route, and returns a similar set of data with the points snapped to the most likely roads the vehicle was traveling along.

that 所示回答。如果您的路线路径有多个点,您需要将其分成 100 个点的部分并单独处理它们(Snap to Road API 也有每个用户 (IP) 每天 2500 个请求和每秒 10 个请求的限制)。

Jaswant Singh回答你:

need to set a custom marker (with icon same as that blue arrow) on your current location and move it to the new location every time there is onLocationChanged() callback is called (Also animate the camera to that new location).

此外,您还需要根据当前用户速度等情况选择 zoomtilt 属性:当用户驾驶速度更快时 tilt -> 0。等等。这不是一个简单的任务。

关于android - 在 Google map 上移动并显示驾驶摄像头 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58451963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com