在Gmap(谷歌地图Android API)中绘制路线坐标
我目前正在使用谷歌地图开发一个Android应用程序。 我的要求是在源-目的地和绘图标记之间每隔500米绘制一条路线。 我画了一条路线,但不知道如何在每500米处绘制标记。是否有任何Google API可用于获取路线上的坐标,或者我必须实现任何其他逻辑?目标 目标是在每N米处获得由方向API web服务返回的路线沿线的LatLng坐标列表。稍后我们可以为这个坐标列表创建标记 解决方案 解决方案有两个步骤。第一个是获取一个LatLng列表,该列表构成了API返回的路线。您可以使用来执行Directions API请求并提取LatLng列表。看看我的示例中的在Gmap(谷歌地图Android API)中绘制路线坐标,android,google-maps,routes,google-maps-android-api-2,Android,Google Maps,Routes,Google Maps Android Api 2,我目前正在使用谷歌地图开发一个Android应用程序。 我的要求是在源-目的地和绘图标记之间每隔500米绘制一条路线。 我画了一条路线,但不知道如何在每500米处绘制标记。是否有任何Google API可用于获取路线上的坐标,或者我必须实现任何其他逻辑?目标 目标是在每N米处获得由方向API web服务返回的路线沿线的LatLng坐标列表。稍后我们可以为这个坐标列表创建标记 解决方案 解决方案有两个步骤。第一个是获取一个LatLng列表,该列表构成了API返回的路线。您可以使用来执行Direct
私有列表getDirectionsPathFromWebService(字符串源、字符串目标)
方法。此方法调用Directions API并循环route对象的分支和步骤,以获得形成路由的LatLng的完整列表
第二步在方法私有列表getMarkersEveryNMeters(列表路径,双距离)
中实现。它循环从第一步开始的所有LatLng,并在每N米处创建一个LatLng列表,其中N是作为该方法的第二个参数传递的以米为单位的距离。此方法在内部使用来自的SphericalUtil
类。请查看注释以了解此方法中发生了什么
最后,我从第二步获得的列表中创建标记
代码片段
公共类MapsActivity扩展了FragmentActivity在MapreadyCallback上的实现{
私有谷歌地图;
私有字符串TAG=“so47784512”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
//获取SupportMapFragment,并在地图准备好使用时收到通知。
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
}
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
mMap=谷歌地图;
字符串原点=“Avinguda对角线,10108005西班牙巴塞罗那”;
字符串目的地=“CARR DER PAR S,67, 08029巴塞罗那,西班牙”;
LatLng中心=新LatLng(41.391942,2.179413);
//定义列表以获取路线的所有板条
列表路径=this.getDirectionsPathFromWebService(源、目标);
//绘制多段线
if(path.size()>0){
PolylineOptions opts=new PolylineOptions().addAll(path).color(color.BLUE).width(5);
mMap.添加多段线(opts);
}
List markers=this.getMarkersEveryNMeters(路径,500.0);
如果(markers.size()>0){
用于(LatLng m:标记){
MarkerOptions mopts=新MarkerOptions().位置(m);
mMap.添加标记(mopts);
}
}
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(中,13));
}
私有列表getDirectionsPathFromWebService(字符串源、字符串目标){
列表路径=新的ArrayList();
//执行方向API请求
GeoApiContext context=新建GeoApiContext.Builder()
.apiKey(“AIzaSyBrPt88vvoPDDn_imh-RZCSL5HA2F2LYIG”)
.build();
DirectionsApi请求req=DirectionsApi.getDirections(上下文、原点、目标);
试一试{
方向结果res=req.await();
//循环遍历腿和步骤,以获得每个步骤的编码多段线
如果(res.routes!=null&&res.routes.length>0){
DirectionsRoute=res.routes[0];
if(route.legs!=null){
对于(int i=0;我看一下这个,我认为它与你的问题陈述类似。
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private String TAG = "so47784512";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
String origin = "Avinguda Diagonal, 101, 08005 Barcelona, Spain";
String destination = "Carrer de París, 67, 08029 Barcelona, Spain";
LatLng center = new LatLng(41.391942,2.179413);
//Define list to get all latlng for the route
List<LatLng> path = this.getDirectionsPathFromWebService(origin, destination);
//Draw the polyline
if (path.size() > 0) {
PolylineOptions opts = new PolylineOptions().addAll(path).color(Color.BLUE).width(5);
mMap.addPolyline(opts);
}
List<LatLng> markers = this.getMarkersEveryNMeters(path, 500.0);
if (markers.size() > 0) {
for (LatLng m : markers) {
MarkerOptions mopts = new MarkerOptions().position(m);
mMap.addMarker(mopts);
}
}
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(center, 13));
}
private List<LatLng> getDirectionsPathFromWebService(String origin, String destination) {
List<LatLng> path = new ArrayList();
//Execute Directions API request
GeoApiContext context = new GeoApiContext.Builder()
.apiKey("AIzaSyBrPt88vvoPDDn_imh-RzCXl5Ha2F2LYig")
.build();
DirectionsApiRequest req = DirectionsApi.getDirections(context, origin, destination);
try {
DirectionsResult res = req.await();
//Loop through legs and steps to get encoded polylines of each step
if (res.routes != null && res.routes.length > 0) {
DirectionsRoute route = res.routes[0];
if (route.legs !=null) {
for(int i=0; i<route.legs.length; i++) {
DirectionsLeg leg = route.legs[i];
if (leg.steps != null) {
for (int j=0; j<leg.steps.length;j++){
DirectionsStep step = leg.steps[j];
if (step.steps != null && step.steps.length >0) {
for (int k=0; k<step.steps.length;k++){
DirectionsStep step1 = step.steps[k];
EncodedPolyline points1 = step1.polyline;
if (points1 != null) {
//Decode polyline and add points to list of route coordinates
List<com.google.maps.model.LatLng> coords1 = points1.decodePath();
for (com.google.maps.model.LatLng coord1 : coords1) {
path.add(new LatLng(coord1.lat, coord1.lng));
}
}
}
} else {
EncodedPolyline points = step.polyline;
if (points != null) {
//Decode polyline and add points to list of route coordinates
List<com.google.maps.model.LatLng> coords = points.decodePath();
for (com.google.maps.model.LatLng coord : coords) {
path.add(new LatLng(coord.lat, coord.lng));
}
}
}
}
}
}
}
}
} catch(Exception ex) {
Log.e(TAG, ex.getLocalizedMessage());
}
return path;
}
private List<LatLng> getMarkersEveryNMeters(List<LatLng> path, double distance) {
List<LatLng> res = new ArrayList();
LatLng p0 = path.get(0);
res.add(p0);
if (path.size() > 2) {
//Initialize temp variables for sum distance between points and
//and save the previous point
double tmp = 0;
LatLng prev = p0;
for (LatLng p : path) {
//Sum the distance
tmp += SphericalUtil.computeDistanceBetween(prev, p);
if (tmp < distance) {
//If it is less than certain value continue sum
prev = p;
continue;
} else {
//If distance is greater than certain value lets calculate
//how many meters over desired value we have and find position of point
//that will be at exact distance value
double diff = tmp - distance;
double heading = SphericalUtil.computeHeading(prev, p);
LatLng pp = SphericalUtil.computeOffsetOrigin(p, diff, heading);
//Reset sum set calculated origin as last point and add it to list
tmp = 0;
prev = pp;
res.add(pp);
continue;
}
}
//Add the last point of route
LatLng plast = path.get(path.size()-1);
res.add(plast);
}
return res;
}
}