Android 如何从谷歌地图标记获取视图?
我想将showcaseview添加到android地图上的标记中!? showcase库将视图作为参数。 如何从标记获取视图 我正在尝试这个代码Android 如何从谷歌地图标记获取视图?,android,android-studio,google-maps,google-maps-markers,google-maps-android-api-2,Android,Android Studio,Google Maps,Google Maps Markers,Google Maps Android Api 2,我想将showcaseview添加到android地图上的标记中!? showcase库将视图作为参数。 如何从标记获取视图 我正在尝试这个代码 new GuideView.Builder(this) .setTitle("Guide Title Text") .setContentText("Guide Description Text\n .....Guide Description Text\n .....Guide Description T
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中的参数,而不是标记对象
这是我想用的图书馆
标记
不是视图,您无法从中获取视图
。但无论如何,您可以创建虚拟透明视图
,并以编程方式将其放置在所选标记上,然后通过setTargetView
方法将其传递到ShowCaseView库。因此,您需要在MapFragment
或MapView
周围使用根视图,例如RelativeLayout
(activity\u main.xml
):
然后,在例如MainActivity
中,您需要获取根布局和充气虚拟透明视图,当选择标记时(例如单击),您应该获取所选标记的当前屏幕坐标,并将虚拟透明视图正好放在其上方。对于RelativeLayout
可以通过setLeft()
对于x
-坐标和setTop()
对于y
-坐标(当然,您需要根据标记调整偏移量)。诸如此类:
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));
...
}
});
}
...
当然,这只是想法的一个例子,您应该改进标记大小、锚定等代码
<?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>
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));
...
}
});
}
...