简体   繁体   English

如何在谷歌地图上创建自己的自定义标记,如谷歌地图的默认标记

[英]How can I create my own custom marker on google map like google map's default marker

I am adding 2 custom markers on the map and finding the route between them.我在地图上添加 2 个自定义标记并找到它们之间的路线。 I am getting route from destination API and it works fine.我正在从目标 API 获取路线,并且工作正常。 But the problem is when this route is drawn on the map my custom markers position and drawn polyline is not 100% correct.但问题是当这条路线在地图上绘制时,我的自定义标记位置和绘制的折线不是 100% 正确的。

I am adding custom marker using this code:我正在使用此代码添加自定义标记:

MarkerOptions().position(startPos).title("Start Point") .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_pin_start)))

and

MarkerOptions().position(endPos).title("End Point") .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_pin_end))

But when I am using default markers everything works fine as I want.但是当我使用默认标记时,一切正常。

MarkerOptions().position(endPos).title("End Point") .icon(BitmapDescriptorFactory.defaultMarker())

When I am adding my custom markers They are shown on the map like this As you can see from this image my circular marker is not pinned to the correct position当我添加自定义标记时,它们会像这样显示在地图上正如您从这张图片中看到的,我的圆形标记没有固定到正确的位置

But actually I want like this但实际上我想要这样

I Think You need to add an anchor point to your marker using this this method in MarkerOptions class我认为您需要在 MarkerOptions 类中使用此方法向标记添加锚点

Definition of this method该方法的定义

Specifies the anchor to be at a particular point in the marker image.指定锚点位于标记图像中的特定点。

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

For example, to set the anchor point on the center of the icon you need to do:例如,要在图标的中心设置锚点,您需要执行以下操作:

markerOptions.anchor(0.2f, 0.2f);

adjust this anchor point according to your need and it will work.根据您的需要调整此锚点,它将起作用。

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

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