android - 如何从谷歌地图标记中获取视图?
问题描述
我想将showcaseview添加到android地图上的标记!?展示库以视图为论据。如何从标记中获取视图?
我正在尝试这段代码
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
解决方案
Marker
不是视图,您无法从中获得View
。但无论如何,您可以创建虚拟透明View
并将其以编程方式放置在选定的标记上,然后通过setTargetView
方法将其传递给 ShowCaseView 库。因此,您需要围绕MapFragment
or的根视图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_width="match_parent"
android:layout_height="match_parent"
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_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
和虚拟透明视图布局(transparent_layout.xml
):
<?xml version="1.0" encoding="utf-8"?>
<View
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="20dp" <- adjust size programmatically or set to default size of Marker
android:layout_height="30dp"
android:layout_centerInParent="true"
android:background="@android:color/transparent">
</View>
Than, in eg MainActivity
, you need get root layout and inflate dummy transparent view, and when marker selected (eg on click) you should get current screen coordinates of the selected Marker
and place dummy transparent view exactly over it. 因为RelativeLayout
它可以通过setLeft()
for x
-coordinate 和setTop()
for 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));
...
}
});
}
...
当然这只是一个想法的例子,你应该改进标记大小、锚点等的代码。
推荐阅读
- cyber-ark - Cyberark:查看账号密码是静态还是动态
- vuetify.js - Vuetify Navigation Drawer 出现裁剪和抽屉大小的问题
- php - Symfony 5.1 - 在提交之前动态添加一个 CustomType 字段
- sqlite - 在 Flutter 应用程序中导入 sqlite 数据库文件
- mysql - 在 mysql 工作台中启动迁移向导时出现问题
- excel - 字符串在一个单元格中搜索多个值
- python - Discord bot 不从外部发送变量
- svg - 在 xaringan/remarkjs 中的“下一张幻灯片”事件中为 SVG 设置动画
- r - 将数据框组合在两个不同的列表中,以 R 中的元素名称为键
- delphi - Delphi TCustomTransparentControl 消失