gpt4 book ai didi

java - 折线不在路上 : it goes straight from one point to other

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:26:18 24 4
gpt4 key购买 nike

我的 map 包含多个点,可以从用户经过的地方点击用户的位置,但是 the polyline is not shown on the roads, but shows a direct line from one marker to other .我希望我的多段线穿过马路:道路转弯时,它也应该转弯。

这是我的相关代码

 dataholder = FirebaseDatabase.getInstance().getReference("UserLocation");
Log.d("onMapReady", "iam here");
dataholder.addValueEventListener(new ValueEventListener() {

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("data",String.valueOf(dataSnapshot.getValue()));
System.out.println(dataSnapshot.getValue());
for (DataSnapshot a : dataSnapshot.getChildren()) {
MapData mapData = a.getValue(MapData.class);
arrayList.add(mapData);
lati.add(arrayList.get(i).getLatituide());
longit.add(arrayList.get(i).getLongitude());
Log.d("mapi","i am in loop");
mMap = googleMap;
Double lat = Double.parseDouble(lati.get(i));
Double longi = Double.parseDouble(longit.get(i));
sydney = new LatLng(longi, lat);
points.add(sydney);
MarkerOptions mop = new MarkerOptions();
mop.position(sydney);
mop.title("check");
mMap.addMarker(mop);
Log.d("latitude", String.valueOf(lat));
Log.d("longitude", String.valueOf(longi));
// Add a marker in Sydney and move the camera
mMap.addMarker(new MarkerOptions().position(sydney).title("check"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));

i++;
}

line.addAll(points).width(5).color(Color.RED);
line.geodesic(true);
mMap.addPolyline(line);

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
Toast.makeText(getApplicationContext(),marker.getTitle().toString(),Toast.LENGTH_LONG).show();
return false;
}
});

最佳答案

尝试使用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.

类似于 Google Maps Roads API Snap to Road 示例中的数据:

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {

private GoogleMap mGoogleMap;
private MapFragment mapFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map_fragment);
mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;

List<LatLng> sourcePoints = new ArrayList<>();
sourcePoints.add(new LatLng(-35.27801,149.12958));
sourcePoints.add(new LatLng(-35.28032,149.12907));
sourcePoints.add(new LatLng(-35.28099,149.12929));
sourcePoints.add(new LatLng(-35.28144,149.12984));
sourcePoints.add(new LatLng(-35.28194,149.13003));
sourcePoints.add(new LatLng(-35.28282,149.12956));
sourcePoints.add(new LatLng(-35.28302,149.12881));
sourcePoints.add(new LatLng(-35.28473,149.12836));

PolylineOptions polyLineOptions = new PolylineOptions();
polyLineOptions.addAll(sourcePoints);
polyLineOptions.width(5);
polyLineOptions.color(Color.BLUE);
mGoogleMap.addPolyline(polyLineOptions);

mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(sourcePoints.get(0), 15));

List<LatLng> snappedPoints = new ArrayList<>();
new GetSnappedPointsAsyncTask().execute(sourcePoints, null, snappedPoints);
}


private String buildRequestUrl(List<LatLng> trackPoints) {
StringBuilder url = new StringBuilder();
url.append("https://roads.googleapis.com/v1/snapToRoads?path=");

for (LatLng trackPoint : trackPoints) {
url.append(String.format("%8.5f", trackPoint.latitude));
url.append(",");
url.append(String.format("%8.5f", trackPoint.longitude));
url.append("|");
}
url.delete(url.length() - 1, url.length());
url.append("&interpolate=true");
url.append(String.format("&key=%s", <your_Google_Maps_API_key>);

return url.toString();
}


private class GetSnappedPointsAsyncTask extends AsyncTask<List<LatLng>, Void, List<LatLng>> {

protected void onPreExecute() {
super.onPreExecute();
}

protected List<LatLng> doInBackground(List<LatLng>... params) {

List<LatLng> snappedPoints = new ArrayList<>();

HttpURLConnection connection = null;
BufferedReader reader = null;

try {
URL url = new URL(buildRequestUrl(params[0]));
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();

InputStream stream = connection.getInputStream();

reader = new BufferedReader(new InputStreamReader(stream));
StringBuilder jsonStringBuilder = new StringBuilder();

StringBuffer buffer = new StringBuffer();
String line = "";

while ((line = reader.readLine()) != null) {
buffer.append(line+"\n");
jsonStringBuilder.append(line);
jsonStringBuilder.append("\n");
}

JSONObject jsonObject = new JSONObject(jsonStringBuilder.toString());
JSONArray snappedPointsArr = jsonObject.getJSONArray("snappedPoints");

for (int i = 0; i < snappedPointsArr.length(); i++) {
JSONObject snappedPointLocation = ((JSONObject) (snappedPointsArr.get(i))).getJSONObject("location");
double lattitude = snappedPointLocation.getDouble("latitude");
double longitude = snappedPointLocation.getDouble("longitude");
snappedPoints.add(new LatLng(lattitude, longitude));
}

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

return snappedPoints;
}

@Override
protected void onPostExecute(List<LatLng> result) {
super.onPostExecute(result);

PolylineOptions polyLineOptions = new PolylineOptions();
polyLineOptions.addAll(result);
polyLineOptions.width(5);
polyLineOptions.color(Color.RED);
mGoogleMap.addPolyline(polyLineOptions);

LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(result.get(0));
builder.include(result.get(result.size()-1));
LatLngBounds bounds = builder.build();
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 10));

}
}

}

并且不要忘记在 Google APIs Console 中为您的项目添加 Google Maps Roads API 支持。 (选择你的项目,然后选择 Dashboard,然后按“+ ENABLE APIS AND SERVICES”按钮,然后在左侧“Filter by”选择“Maps”,然后按“Google Maps Roads API”,最后按“ENABLE”按钮").

你应该得到这样的东西:

Snapped points polyline

其中:蓝色折线 - 用于源点,红色折线 - 用于捕捉点。

请记住,每个用户 (IP) 每天有 100 个 GPS 点和 2500 个请求以及每秒 10 个请求的限制。并且您还需要更优雅的解决方案来下载捕捉点 JSON。

关于java - 折线不在路上 : it goes straight from one point to other,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47329243/

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