我想在我的谷歌地图片段上画一个三角形。 它应该如下所示: i.stack.imgur.com/880kV.png
要创建多边形,我需要3个位置(A,B,C)。 我必须创建的变量是我当前的位置(点A),手机的当前角度,2个较长边(以米为单位)的长度以及它们之间的角度。
首先,我根据纬度
得到1米长的长度ncgia.ucsb.edu/education/curricula/giscc/units/u014/tables/table01.html
double factor = ((double)1)/111230
然后我从北方获得角度(viewAngleTolerance =两条较长线之间的角度):
float angleToLocB = angle - (viewAngleTolerance / 2);
float angleToLocC = angle + (viewAngleTolerance / 2);
http://i.stack.imgur.com/x5VF9.png(对不起油漆技巧感到抱歉)
接下来,我会计算出橙色和绿色线条(a,b),如图所示。
double Ba = 0;
double Bb = 0;
if (angleToLocB >= 0 && angleToLocB <= 90) {
Ba = Math.sin(Math.toRadians(angleToLocB)) * maxDistance * factor;
Bb = Math.sin(Math.toRadians(90 - angleToLocB)) * maxDistance * factor;
} else if (angleToLocB > 90 && angleToLocB <= 180) {
Ba = Math.sin(Math.toRadians(angleToLocB - 90)) * maxDistance * factor;
Bb = Math.sin(Math.toRadians(90 - (angleToLocB - 90))) * maxDistance * factor;
} else if (angleToLocB > 180 && angleToLocB <= 270) {
Ba = Math.sin(Math.toRadians(angleToLocB - 180)) * maxDistance * factor;
Bb = Math.sin(Math.toRadians(90 - (angleToLocB - 180))) * maxDistance * factor;
} else if (angleToLocB > 270 && angleToLocB <= 360) {
Ba = Math.sin(Math.toRadians(angleToLocB - 270)) * maxDistance * factor;
Bb = Math.sin(Math.toRadians(90 - (angleToLocB - 270))) * maxDistance * factor;
}
double Ca = 0;
double Cb = 0;
if (angleToLocC >= 0 && angleToLocC <= 90) {
Ca = Math.sin(Math.toRadians(angleToLocC)) * maxDistance * factor;
Cb = Math.sin(Math.toRadians(90 - angleToLocC)) * maxDistance * factor;
} else if (angleToLocC > 90 && angleToLocC <= 180) {
Ca = Math.sin(Math.toRadians(angleToLocC - 90)) * maxDistance * factor;
Cb = Math.sin(Math.toRadians(90 - (angleToLocC - 90))) * maxDistance * factor;
} else if (angleToLocC > 180 && angleToLocC <= 270) {
Ca = Math.sin(Math.toRadians(angleToLocC - 180)) * maxDistance * factor;
Cb = Math.sin(Math.toRadians(90 - (angleToLocC - 180))) * maxDistance * factor;
} else if (angleToLocC > 270 && angleToLocC <= 360) {
Ca = Math.sin(Math.toRadians(angleToLocC - 270)) * maxDistance * factor;
Cb = Math.sin(Math.toRadians(90 - (angleToLocC - 270))) * maxDistance * factor;
}
之后我计算纬度和长度为B和C.
Location B = new Location("");
Location C = new Location("");
if (angleToLocB > 90 && angleToLocB <= 270) {
B.setLatitude(currentLoc.getLatitude() - Bb);
} else if (angleToLocB > 0 && angleToLocB <= 90) {
B.setLatitude(currentLoc.getLatitude() + Bb);
} else if (angleToLocB > 270 && angleToLocB <= 360) {
B.setLatitude(currentLoc.getLatitude() + Bb);
}
if (angleToLocC > 90 && angleToLocC <= 270) {
C.setLatitude(currentLoc.getLatitude() - Cb);
} else if (angleToLocC > 0 && angleToLocC <= 90) {
C.setLatitude(currentLoc.getLatitude() + Cb);
} else if (angleToLocC > 270 && angleToLocC <= 360) {
C.setLatitude(currentLoc.getLatitude() + Cb);
}
if (angleToLocB > 0 && angleToLocB <= 180) {
B.setLongitude(currentLoc.getLongitude() + Ba);
} else if (angleToLocB > 180 && angleToLocB <= 360) {
B.setLongitude(currentLoc.getLongitude() - Ba);
}
if (angleToLocC > 0 && angleToLocC <= 180) {
C.setLongitude(currentLoc.getLongitude() + Ca);
} else if (angleToLocC > 180 && angleToLocC <= 360) {
C.setLongitude(currentLoc.getLongitude() - Ca);
}
我的最后一步是创建多边形并将其添加到地图中:
polygonOptions = new PolygonOptions()
.add(new LatLng(currentLoc.getLatitude(), currentLoc.getLongitude()),
new LatLng(B.getLatitude(), B.getLongitude()),
new LatLng(C.getLatitude(), C.getLongitude()),
new LatLng(currentLoc.getLatitude(), currentLoc.getLongitude()))
.strokeColor(Color.BLACK)
.fillColor(Color.GRAY);
if(polygon != null) {
if (polygon.isVisible()) {
polygon.remove();
}
}
polygon = map.addPolygon(polygonOptions);
此方法有效但我认为有一个easyer方法来创建此多边形。我的第一个问题是,是否有任何easyer方法。
此外,如果一个角度正好是0,90,180,270或360,或者该点被创建错误。我的第二个问题是为什么会这样。
感谢您的帮助。
答案 0 :(得分:0)
通过导入这三个库,有一种非常简单的方法:
import com.google.android.gms.maps.model.Polygon;
import com.google.android.gms.maps.model.PolygonOptions;
import com.google.android.gms.maps.model.PolylineOptions;
按照步骤tutorial进行操作。