android - 用 Polygon 在整个地图上绘制一个矩形
问题描述
我一直在尝试在 Android Studio 中的整个地图上(在地图活动中)绘制一个矩形,我需要从地图的一部分到另一部分的赤道区域的划界。(在一个大矩形中)但是每次我将矩形的坐标放在相反的位置时,它都会向后移动并从太平洋到中国,澳大利亚再返回一个小正方形。
另外,知道如何让按钮在地图上形成一个国家的形状吗?
package com.example.android.coffeeknowledge;
import android.content.res.Resources;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MapStyleOptions;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polygon;
import com.google.android.gms.maps.model.PolygonOptions;
import com.google.android.gms.maps.model.PolylineOptions;
public class coffeeMap extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_coffee_map);
// 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);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
private static final LatLng cameraZoom = new LatLng(37.35, -122.0);
@Override
public void onMapReady(GoogleMap googleMap) {
try{
boolean success = googleMap.setMapStyle(
MapStyleOptions.loadRawResourceStyle(this, R.raw.mapstyle));
if(!success){
Log.e("coffeeMap","Style parsing failed.");
}
}catch(Resources.NotFoundException e){
Log.e("coffeeMap", "Can`t find style.Error: " , e);
}
mMap = googleMap;
// Instantiates a new Polygon object and adds points to define a rectangle
PolygonOptions rectOptions = new PolygonOptions()
.fillColor(R.color.white)
.add(new LatLng(24.376368, 101.181309),
new LatLng(-28.912738, 103.818027),
new LatLng(-26.841671, -117.944509),
new LatLng(27.616242, -122.020003),
new LatLng(24.376368, 101.181309));
// Get back the mutable Polygon
Polygon polygon = mMap.addPolygon(rectOptions);
// Add a marker in Sydney and move the camera
//mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(cameraZoom, 13));
LatLng sydney = new LatLng(35.175321, -107.619365);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker"));
}
}
谢谢你。
解决方案
考虑到测地线路径或矩形投影,地图 api 将始终选择 2 点之间的最短路线。您的示例中感兴趣的部分是:
new LatLng(-28.912738, 103.818027), // A
new LatLng(-26.841671, -117.944509), // B
和
new LatLng(27.616242, -122.020003), // C
new LatLng(24.376368, 101.181309)) // D
这两个部分将穿过反子午线(而不是相反),因为这是这两点之间的最短路径。(这总是需要的。)
因此,要在您的示例中克服这一点,只需在段(AB 和 CD)中添加一个中间中点(假设为非测地线或矩形投影)以强制它“走另一条路”。以 AB 为例:
new LatLng(-28.912738, 103.818027),
new LatLng(-27.877204, -7.0), // approximate midpoint in desired direction (M)
new LatLng(-26.841671, -117.944509),
所以到 AB 的原始距离(假设测地线)是 12830 公里。与强制中间点是:AM 10320km 和MB 10460km。这些距离计算仅仅是为了证明这一点(双关语)。
同样的方法也适用于 CD。
因此,在图片中,您的 OP 视图使用:
PolygonOptions rectOptions = new PolygonOptions()
.fillColor(R.color.colorPrimary)
.add(new LatLng(24.376368, 101.181309),
new LatLng(-28.912738, 103.818027),
new LatLng(-26.841671, -117.944509),
new LatLng(27.616242, -122.020003),
new LatLng(24.376368, 101.181309));
显示为:
并带有两个中间点:
PolygonOptions rectOptions = new PolygonOptions()
.fillColor(R.color.colorPrimary)
.add(new LatLng(24.376368, 101.181309),
new LatLng(-28.912738, 103.818027),
new LatLng(-27.877204, -7.0),
new LatLng(-26.841671, -117.944509),
new LatLng(27.616242, -122.020003),
new LatLng( 25.9, -7.0),
new LatLng(24.376368, 101.181309));
显示为:
只是为了好玩,并强调中点确定取决于投影,这是使用测地线的相同多边形:
找到跨越大于 pi 弧度的弧的 2 个点的球面中点是另一天的问题......
一个方便的在线工具供更多考虑:https ://www.movable-type.co.uk/scripts/latlong.html 。
推荐阅读
- python - 如何将 N 个多列表合并为一个大的多列表?
- arrays - 快速调用解析函数失败
- r - 如何在 R 中绘制稀疏矩阵?
- r - How do I subset a list with mixed data type and data structure?
- elasticsearch - Elasticsearch term query to number token
- angular - 网站没有在服务器上加载,同时从浏览器以角度 2 刷新它
- python - 如何读取缺少值和'delim_whitespace = True'的csv文件?
- javascript - Change state/prop in video react-native-video component
- python - 有没有办法可以在 sqlite3 数据库中找到随机表的同一行?
- html - 当窗口变小时如何修复网站中的空白