Android GoogleMap V2:如何绘制特定长度的线?
Android GoogleMap V2 : How can I draw a line with specific length?
我的项目中有一个 GoogleMap。它设置为 18 级缩放。我想画一条 1 米长的线。我看到并使用了这样的代码:
googleMap.addCircle(new CircleOptions()
.center(latLng1)
.radius(5)
.fillColor(Color.BLUE));
我给出了它的半径(以米为单位)。我怎样才能用一条线来做到这一点?(polyLine 没有这个选项)具有特定 LatLng 和特定方向(例如:从北向)和特定长度的线?
我可以通过 sin 和 cos 指定方向..但是我能为线的长度做什么?
使用折线画线,如下所示,
private ArrayList<LatLng> mLatlngs ;
PolylineOptions mPolylineOptions = new PolylineOptions();
mPolylineOptions.color(Color.RED);
mPolylineOptions.width(3);
mPolylineOptions.addAll(mLatlngs);
mGoogleMap.addPolyline(mPolylineOptions);
对于给定的点,只有一个给定半径的圆。但是对于线条,情况有点不同。对于给定点,从该点开始并给定长度有无数条线。所以你不能简单地画出这样的线。
一种方法是在半径为 1 米的圆上选择一个点并将您的点居中。 Here 是一个很好的例子,如何计算给定半径的圆上的点。不仅仅是在两点之间画一条线。
更新:
这可能会帮助您找到圆上的 LatLng 点 LatLng Points on circle on Google Map V2 in Android
为了计算行尾,我使用:
SphericalUtil.computeOffset(LatLng,lenght,heading);
为了计算以米为单位的宽度,我使用了这个:
public Double calcw(GoogleMap map,int ancho,LatLng p) {
float tilt = map.getCameraPosition().tilt;
CameraPosition old=map.getCameraPosition();
if(tilt!=0){
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(old.target) // Sets the center of the map to Mountain View
.zoom(old.zoom) // Sets the zoom
.bearing(old.bearing) // Sets the orientation of the camera to east
.tilt(0) // Sets the tilt of the camera to 30 degrees
.build();
map.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
Point g1 =map.getProjection().toScreenLocation(p);
Point g2=map.getProjection().toScreenLocation(SphericalUtil.computeOffset(p,ancho/10,0));
Double result=(distance(g1,g2));
//Log.e("PROJ1",Double.toString(distance(g1,g2)));
map.moveCamera(CameraUpdateFactory.newCameraPosition(old));
return result;
}
public double distance(Point a, Point b)
{
double dx = a.x - b.x;
double dy = a.y - b.y;
return Math.sqrt(dx * dx + dy * dy);
}
我的项目中有一个 GoogleMap。它设置为 18 级缩放。我想画一条 1 米长的线。我看到并使用了这样的代码:
googleMap.addCircle(new CircleOptions()
.center(latLng1)
.radius(5)
.fillColor(Color.BLUE));
我给出了它的半径(以米为单位)。我怎样才能用一条线来做到这一点?(polyLine 没有这个选项)具有特定 LatLng 和特定方向(例如:从北向)和特定长度的线? 我可以通过 sin 和 cos 指定方向..但是我能为线的长度做什么?
使用折线画线,如下所示,
private ArrayList<LatLng> mLatlngs ;
PolylineOptions mPolylineOptions = new PolylineOptions();
mPolylineOptions.color(Color.RED);
mPolylineOptions.width(3);
mPolylineOptions.addAll(mLatlngs);
mGoogleMap.addPolyline(mPolylineOptions);
对于给定的点,只有一个给定半径的圆。但是对于线条,情况有点不同。对于给定点,从该点开始并给定长度有无数条线。所以你不能简单地画出这样的线。
一种方法是在半径为 1 米的圆上选择一个点并将您的点居中。 Here 是一个很好的例子,如何计算给定半径的圆上的点。不仅仅是在两点之间画一条线。
更新:
这可能会帮助您找到圆上的 LatLng 点 LatLng Points on circle on Google Map V2 in Android
为了计算行尾,我使用:
SphericalUtil.computeOffset(LatLng,lenght,heading);
为了计算以米为单位的宽度,我使用了这个:
public Double calcw(GoogleMap map,int ancho,LatLng p) {
float tilt = map.getCameraPosition().tilt;
CameraPosition old=map.getCameraPosition();
if(tilt!=0){
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(old.target) // Sets the center of the map to Mountain View
.zoom(old.zoom) // Sets the zoom
.bearing(old.bearing) // Sets the orientation of the camera to east
.tilt(0) // Sets the tilt of the camera to 30 degrees
.build();
map.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
Point g1 =map.getProjection().toScreenLocation(p);
Point g2=map.getProjection().toScreenLocation(SphericalUtil.computeOffset(p,ancho/10,0));
Double result=(distance(g1,g2));
//Log.e("PROJ1",Double.toString(distance(g1,g2)));
map.moveCamera(CameraUpdateFactory.newCameraPosition(old));
return result;
}
public double distance(Point a, Point b)
{
double dx = a.x - b.x;
double dy = a.y - b.y;
return Math.sqrt(dx * dx + dy * dy);
}