折线不在道路上:它从一个点直接到另一个点
Posted
技术标签:
【中文标题】折线不在道路上:它从一个点直接到另一个点【英文标题】:PolyLine is not on the roads: it goes straight from one point to other 【发布时间】:2018-04-29 23:12:27 【问题描述】:我的地图包含多个点来点击用户经过的位置,但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;
);
【问题讨论】:
androidtutorialpoint.com/intermediate/… 试试这个教程来绘制道路 【参考方案1】:尝试使用Google Maps Roads API part Snap to Road which
针对给定的 GPS 坐标集返回最适合的道路几何形状。 该服务最多需要沿路线收集 100 个 GPS 点,并且 返回一组相似的数据,其中的点捕捉到最多 车辆行驶的可能道路。
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 支持(选择您的项目,然后选择仪表板,然后按“+启用 APIS 和服务”按钮,而不是左侧的“过滤方式” 选择“地图”,然后按“Google Maps Roads API”,最后按“启用”按钮。
你应该得到类似的东西:
其中:蓝色折线 - 用于源点,红色折线 - 用于捕捉点。
请记住,每个用户 (IP) 每天最多 100 个 GPS 点和 2500 个请求,每秒 10 个请求。而且您还需要更优雅的解决方案来下载快照点 JSON。
【讨论】:
@RanaArslan Here 是有人使用它的一个例子。 @TobiasHvassMølbak 该示例适用于 Directions API,而不适用于 Roads API。 @TobiasHvassMølbak 那么如何拥有道路 API? @RanaArslan 只需转到Google APIs Console 选择您的项目,然后选择仪表板,然后按“+ ENABLE APIS AND SERVICES”按钮,然后在左侧“过滤依据”选择“地图”,然后按“Google Maps Roads API”,最后按“启用”按钮。 @AndriiOmelchenko 它没有映射道路上的点检查更新的问题输出已附加以上是关于折线不在道路上:它从一个点直接到另一个点的主要内容,如果未能解决你的问题,请参考以下文章