【问题标题】:Drawing a rectangle on the whole map with Polygon用 Polygon 在整个地图上绘制一个矩形
【发布时间】:2018-08-01 11:38:05
【问题描述】:

我一直在尝试在 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"));
 }
 }

谢谢。

【问题讨论】:

    标签: android google-maps drawing polygon


    【解决方案1】:

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

    这两个部分将穿过反子午线(而不是相反),因为这是这两点之间的最短路径。 (这总是需要的。)

    因此,要在您的示例中克服这个问题,只需在线段(A-B 和 C-D)中添加一个中间中点(假设为非测地线或矩形投影),以强制它“走另一条路”。以 A-B 为例:

    new LatLng(-28.912738, 103.818027),
    new LatLng(-27.877204, -7.0),        // approximate midpoint in desired direction (M)
    new LatLng(-26.841671, -117.944509),
    

    所以从 A-B 的原始距离(假设测地线)是 12830 公里。而强制中间点是:A-M 10320km 和 M-B 10460km。这些距离计算只是为了证明这一点(双关语)。

    同样的方法也适用于 C-D。


    所以在图片中,您的 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

    【讨论】:

    • 你知道如何勾勒出一个大陆或国家吗?是否有命令或者我需要使用多边形手动执行?
    • @Daudjawad api 不这样做;您可以从以下网站获取国家边界数据:gadm.org/download_country_v3.html -(选择国家并选择 kml。然后捕获 kml 以转换为折线点列表)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多