如何从谷歌地图标记中获取视图?
Posted
技术标签:
【中文标题】如何从谷歌地图标记中获取视图?【英文标题】:How to get View from google maps Marker? 【发布时间】:2021-01-17 02:08:13 【问题描述】:我想将showcaseview 添加到android 地图上的标记!? 展示库以视图为论据。 如何从 Marker 获取视图?
我正在尝试这段代码
new GuideView.Builder(this)
.setTitle("Guide Title Text")
.setContentText("Guide Description Text\n .....Guide Description Text\n .....Guide Description Text .....")
.setGravity(Gravity.auto) //optional
.setDismissType(DismissType.anywhere) //optional - default DismissType.targetView
.setTargetView(view)
.setContentTextSize(12)//optional
.setTitleTextSize(14)//optional
.build()
.show();
它在 setTargetView 中将视图作为参数,而不是标记对象
这是我想使用的库 https://github.com/mreram/ShowCaseView
【问题讨论】:
【参考方案1】:Marker
不是视图,您无法从中获取View
。但无论如何,您可以创建虚拟透明 View
并将其以编程方式放置在选定的标记上,然后通过 setTargetView
方法将其传递给 ShowCaseView 库。因此,您需要围绕 MapFragment
或 MapView
的根视图,例如 RelativeLayout
(activity_main.xml
):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mapview_root"
android:layout_
android:layout_
tools:context="<YOUR_PACKAGE_NAME>.MainActivity">
<com.google.android.gms.maps.MapView android:id="@+id/mapview"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
/>
</RelativeLayout>
和虚拟透明视图布局 (transparent_layout.xml
):
<?xml version="1.0" encoding="utf-8"?>
<View
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_ <- adjust size programmatically or set to default size of Marker
android:layout_
android:layout_centerInParent="true"
android:background="@android:color/transparent">
</View>
比,例如MainActivity
,您需要获取根布局并膨胀虚拟透明视图,以及选择标记时(例如,单击)时,您应该获取所选Marker
的当前屏幕坐标,并将虚拟透明视图放在完全覆盖。对于RelativeLayout
,可以通过setLeft()
来完成x
-coordinate 和setTop()
对于y
-coordinate(当然你需要根据Marker
锚点调整偏移量)。类似的东西:
public class MainActivity extends AppCompatActivity
private static final String MAP_VIEW_BUNDLE_KEY = "MapViewBundleKey";
static final LatLng KYIV = new LatLng(50.450311, 30.523730);
private GoogleMap mGoogleMap;
private RelativeLayout mMapViewRoot;
private MapView mGoogleMapView;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle mapViewBundle = null;
if (savedInstanceState != null)
mapViewBundle = savedInstanceState.getBundle(MAP_VIEW_BUNDLE_KEY);
mMapViewRoot = (RelativeLayout) findViewById(R.id.mapview_root);
// dummy transparent view
final View transparentView = View.inflate(getApplicationContext(), R.layout.transparent_view, mMapViewRoot);
mGoogleMapView = (MapView) findViewById(R.id.mapview);
mGoogleMapView.onCreate(mapViewBundle);
mGoogleMapView.getMapAsync(new OnMapReadyCallback()
@Override
public void onMapReady(GoogleMap googleMap)
mGoogleMap = googleMap;
mGoogleMap.addMarker(new MarkerOptions().position(KYIV).title("Kyiv"));
mGoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener()
@Override
public boolean onMarkerClick(Marker marker)
// get screen coordinates of the marker
Projection projection = mGoogleMap.getProjection();
Point viewPosition = projection.toScreenLocation(marker.getPosition());
// place dummy transparent view over the marker
transparentView.setLeft(viewPosition.x);
transparentView.setTop(viewPosition.y);
return false;
);
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLng(KYIV));
...
);
...
当然这只是一个想法的例子,你应该改进标记大小、锚点等的代码。
【讨论】:
以上是关于如何从谷歌地图标记中获取视图?的主要内容,如果未能解决你的问题,请参考以下文章