使用Polygon在整个地图上绘制一个矩形

时间:2018-08-01 11:38:06

标签: android google-maps drawing 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"));
 }
 }

images

谢谢。

1 个答案:

答案 0 :(得分:2)

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

显示为:

enter image description here

以及2个中间点:

    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));

显示为:

enter image description here

只是为了好玩,并强调中点确定取决于投影,以下是使用测地线的同一多边形:

enter image description here

再过一天,要找到一个跨度大于pi弧度的圆弧的2个点是球的中点...

方便使用的在线工具:https://www.movable-type.co.uk/scripts/latlong.html