简体   繁体   English

Android GoogleMap一个正方形出现矩形

[英]Android GoogleMap a square appears rectangle

I've created a GoogleMap object and drawn a polygon with a square vertex as shown in the figure, but the square appears an oblong. 我创建了一个GoogleMap对象,并绘制了一个带有方形顶点的多边形,如图所示,但是方形看起来是椭圆形的。 What am I doing wrong? 我究竟做错了什么?

This is a fragment of the code: 这是代码的一个片段:

@Override
public void onMapReady(GoogleMap googleMap) {

    mMap = googleMap;


    // Add a marker in Sydney and move the camera
    LatLng enidh = new LatLng(38.6925785, -9.2955145);
    mMap.addMarker(new MarkerOptions().position(enidh).title("Marker in ENIDH"));
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(enidh, 10.0f));


    mBoats[0] = mMap.addPolygon(new PolygonOptions()
            .add(new LatLng(38.680026, -9.2846651),
                    new LatLng(38.690026, -9.2846651),
                    new LatLng(38.690026, -9.2946651),
                    new LatLng(38.680026, -9.2946651),
                    new LatLng(38.680026, -9.2846651))
            .fillColor(Color.CYAN)
            .strokeColor(Color.BLUE)
            .strokeWidth(5));

This is the square (rectangle) appearance in android app emulator 这是android app模拟器中的方形(矩形)外观

You've specified coordinates of the Polygon using LatLng . 您已使用LatLng指定了Polygon坐标。 However the distances (ie in km) represented by 1 longitude and 1 latitude are not the same : 但是,由1个经度和1个纬度表示的距离(即以km为单位) 并不相同

Each degree of latitude is approximately 69 miles (111 kilometers) apart. 每个纬度相距约69英里(111公里)。 The range varies (due to the earth's slightly ellipsoid shape) from 68.703 miles (110.567 km) at the equator to 69.407 (111.699 km) at the poles. 范围从赤道的68.703英里(110.567公里)到极地的69.407(111.699公里)不等(由于地球略呈椭圆形)。 This is convenient because each minute (1/60th of a degree) is approximately one mile. 这很方便,因为每分钟(1/60度)大约是一英里。

A degree of longitude is widest at the equator at 69.172 miles (111.321) and gradually shrinks to zero at the poles. 赤道的经度最宽,为69.172英里(111.321),在极地逐渐缩小为零。 At 40° north or south the distance between a degree of longitude is 53 miles (85 km). 在北纬40°或北纬经度之间的距离为53英里(85公里)。

Thus even though the edges lengths of polygon you've created have the same unit-less value you get a rectangle that is not a square. 因此,即使您创建的多边形的边长度具有相同的无单位值,您也会得到一个不是正方形的矩形。

You are not adding polygon properly. 您没有正确添加多边形。 You have to use 4 different coordinates as corners of rectangle like this. 你必须使用4个不同的坐标作为矩形的角。

// Instantiates a new Polyline object and adds points to define a rectangle
PolylineOptions rectOptions = new PolylineOptions()
        .add(new LatLng(37.35, -122.0))
        .add(new LatLng(37.45, -122.0))  // North of the previous point, but at the same longitude
        .add(new LatLng(37.45, -122.2))  // Same latitude, and 30km to the west
        .add(new LatLng(37.35, -122.2))  // Same longitude, and 16km to the south
        .add(new LatLng(37.35, -122.0)); // Closes the polyline.

// Get back the mutable Polyline
Polyline polyline = myMap.addPolyline(rectOptions);

Source : https://developers.google.com/maps/documentation/android-api/shapes 资料来源: https//developers.google.com/maps/documentation/android-api/shapes

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

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