简体   繁体   English

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

[英]Drawing a rectangle on the whole map with Polygon

I have been trying to draw a rectangle on the whole map(in a map activity) in Android Studio, i need a delimitation of the equator area, from one part of the map to the other. 我一直试图在Android Studio中在整个地图上(在地图活动中)绘制一个矩形,我需要划定从地图的一部分到另一部分的赤道区域。 ( in a big rectangle) But every time i put the coordinates for the rectangle it goes the other way around, so it goes backwards and makes a small square from Pacific ocean to China , Australia and back. (在一个大矩形中)但是每当我放置该矩形的坐标时,它都会沿相反方向移动,因此它将向后移动,并从太平洋到中国,澳大利亚再返回一个小正方形。

Also, any idea how i can make a button take the shape of a country on the map? 另外,有什么想法可以使按钮在地图上呈现一个国家的形状吗?

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

图片

Thank you. 谢谢。

The map api will always choose the shortest route between 2 points taking into account geodesic path or rectangular projection. 考虑到测地线路径或矩形投影,地图api始终会选择2个点之间的最短路径。 The segments of interest in your example are: 您的示例中感兴趣的细分为:

new LatLng(-28.912738, 103.818027),   // A
new LatLng(-26.841671, -117.944509),  // B

and

new LatLng(27.616242, -122.020003),   // C
new LatLng(24.376368, 101.181309))    // D

These two segments will cross the anti-meridian (and not go the other way around) because that is the shortest path between those two points. 这两个线段将越过反子午线(并且不会相反),因为这是这两点之间的最短路径。 (This is always what would be desired.) (这总是需要的。)

So to overcome this in your example, simply add an intermediate mid-point (assuming non-geodesic, or rectangular projection) in the segments (AB and CD) to force it to go "the other way". 因此,要在您的示例中克服这一点,只需在线段(AB和CD)中添加一个中间的中点(假定非大地测量或矩形投影),以迫使其“朝另一方向走”。 Using AB as an example: 以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),

So the original distance from AB (assuming geodesic) is 12830 km. 因此,距AB(假设测地线)的原始距离为12830 km。 And with the forced intermediate point is: AM 10320km and MB 10460km. 强制中间点为:AM 10320km和MB 10460km。 These distance calculations are merely to demonstrate the point (pun intended). 这些距离计算仅用于说明该点(双关目标)。

The same approach applies to CD. 相同的方法适用于CD。


So in pictures, your OP view using: 因此,在图片中,您的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));

is displayed as: 显示为:

在此处输入图片说明

and with the 2 intermediate points: 并具有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));

is displayed as: 显示为:

在此处输入图片说明

Just for fun, and to emphasize the midpoint determination depends on the projection, here is the same polygon using geodesic: 只是为了好玩,并强调中点确定取决于投影,以下是使用测地线的同一多边形:

在此处输入图片说明

Finding the spherical midpoint for 2 points spanning an arc greater than pi radians is a problem for another day... 再过一天,要找到跨越大于pi弧度的弧的2个点的球面中点是一个问题...

A handy online tool for more consideration: https://www.movable-type.co.uk/scripts/latlong.html . 一个方便在线的工具,需要更多考虑: https : //www.movable-type.co.uk/scripts/latlong.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM