简体   繁体   English

带有折线的自定义Google Maps标记

[英]Custom Google maps marker with polyline

I want to join two markers on my Google Map with a polyline. 我想在我的Google地图上用折线连接两个标记。 Here is my code : 这是我的代码:

mMap.addMarker(new MarkerOptions()
            .position(new LatLng(0, 0))
            .title("Pos")
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.point)));

mMap.addPolyline(new PolylineOptions()
            .add(new LatLng(0,0), new LatLng(20,20))
            .width(5)
            .color(Color.rgb(255, 161, 74)));

The polyline works but with my custom marker, i have a margin between the line and the marker. 折线有效,但是使用我的自定义标记,我在线条和标记之间留有边距。

在此处输入图片说明

I would like to remove this margin. 我想删除此边距。 How can I do that ? 我怎样才能做到这一点 ?

Thank you :) 谢谢 :)

The problem is that by default the anchor point for the marker is centered at the bottom of the image. 问题是默认情况下,标记的锚点位于图像底部的中心。 If you looked closely at the locations of your custom markers, you would notice that they are not centered on the lat/long you set them to, but are just above the location you set. 如果仔细查看自定义标记的位置,您会注意到它们不在您设置的纬度/经度中心,而是在所设置的位置上方。

if you set the anchor to 0.5, 0.5 you should get the desired affect. 如果将锚点设置为0.5,则0.5将获得所需的效果。

Here is info from the document, which can also be found here 这是文档中的信息,也可以在这里找到

public void setAnchor (float anchorU, float anchorV) public void setAnchor(浮动锚U,浮动锚V)

Sets the anchor point for the marker. 设置标记的锚点。

The anchor specifies the point in the icon image that is anchored to the marker's position on the Earth's surface. 锚点指定图标图像中锚定到地球表面上标记位置的点。

The anchor point is specified in the continuous space [0.0, 1.0] x [0.0, 1.0], where (0, 0) is the top-left corner of the image, and (1, 1) is the bottom-right corner. 在连续空间[0.0,1.0] x [0.0,1.0]中指定锚点,其中(0,0)是图像的左上角,而(1,1)是右下角。 The anchoring point in a W x H image is the nearest discrete grid point in a (W + 1) x (H + 1) grid, obtained by scaling the then rounding. W x H图像中的锚点是(W + 1)x(H +1)网格中最接近的离散网格点,它是通过缩放然后舍入获得的。 For example, in a 4 x 2 image, the anchor point (0.7, 0.6) resolves to the grid point at (3, 1). 例如,在4 x 2图像中,锚点(0.7,0.6)解析为(3,1)处的网格点。

 *-----+-----+-----+-----*
 |     |     |     |     |
 |     |     |     |     |
 +-----+-----+-----+-----+
 |     |     |   X |     |   (U, V) = (0.7, 0.6)
 |     |     |     |     |
 *-----+-----+-----+-----*

 *-----+-----+-----+-----*
 |     |     |     |     |
 |     |     |     |     |
 +-----+-----+-----X-----+   (X, Y) = (3, 1)
 |     |     |     |     |
 |     |     |     |     |
 *-----+-----+-----+-----*

Parameters anchorU u-coordinate of the anchor, as a ratio of the image width (in the range [0, 1]) anchorV v-coordinate of the anchor, as a ratio of the image height (in the range [0, 1]) 参数anchorU u坐标,作为图像宽度的比率(在[0,1]范围内)anchorV v坐标,作为图像高度的比率(在[0,1]范围内) )

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

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