首页 > 解决方案 > 用 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"));
 }
 }

图片

谢谢你。

标签: androidgoogle-mapsdrawingpolygon

解决方案


考虑到测地线路径或矩形投影,地图 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 。


推荐阅读