在标记的Android弹出或布局 [英] Popup or layout over marker Android
问题描述
我想提出一个自定义布局在一个标记。到目前为止,我得到这个:
default_marker_info_window.xml
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直> < ViewFlipper
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID / markerInfoContainer
机器人:背景=@机器人:彩色/ holo_red_dark
安卓了maxWidth =250dp>
< / ViewFlipper> < ImageView的
机器人:layout_width =20dp
机器人:layout_height =15dp
机器人:背景=@绘制/ ic_down_arrow
机器人:layout_gravity =中心|顶/>< / LinearLayout中>
default_marker_info_layout.xml
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直的android:layout_width =match_parent
机器人:layout_height =match_parent> <的LinearLayout
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直> <的TextView
机器人:ID =@ + ID / tvTitulo
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:文字样式=大胆
机器人:比重=中心/> <的TextView
机器人:ID =@ + ID / tvCuerpo
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT/> <按钮
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=preSS我1
机器人:ID =@ + ID /键/> <按钮
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=preSS我2
机器人:ID =@ + ID /按钮2/> < / LinearLayout中>< / LinearLayout中>
MapActivity.java
公共类MapActivity扩展活动实现GoogleMap.OnMapLongClickListener { 保护GoogleMap的MMAP; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
...
setUpMapIfNeeded();
mMap.setOnMarkerClickListener(新GoogleMap.OnMarkerClickListener(){
@覆盖
公共布尔onMarkerClick(标记标记){ 查看MAINVIEW = getLayoutInflater()膨胀(R.layout.default_marker_info_window,NULL);
ViewFlipper markerInfoContainer =(ViewFlipper)mainView.findViewById(R.id.markerInfoContainer);
查看viewContainer = getLayoutInflater()膨胀(R.layout.default_marker_info_layout,NULL);
TextView的tvTitulo =(TextView中)viewContainer.findViewById(R.id.tvTitulo);
TextView的tvCuerpo =(TextView中)viewContainer.findViewById(R.id.tvCuerpo);
tvTitulo.setText(marker.getTitle());
tvCuerpo.setVisibility(View.GONE); markerInfoContainer.addView(viewContainer); PopupWindow popupWindow =新PopupWindow(MAINVIEW,ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.showAtLocation(findViewById(R.id.map),Gravity.CENTER_HORIZONTAL,0,0); //地图上的活动布局,我把地图中的片段 返回true;
}
});
...
}
...
}
不过,我有几个问题。
问题1:弹出(3D)的标记(hidding吧)显示了。我想将它定位它(2D)以上的,就像这样:
位置>可通过帮助 X
和是
标记中的位置,并使用它们 popupWindow.showAtLocation
。但是,如果你有其他的解决方案,我会听他们的。
问题2:当我滑动地图上,布局上保持画面的中心,但我要保持它在标记
请注意:我不能使用 mMap.setInfoWindowAdapter
,因为我会失去按钮的点击监听器
定义一些变量保存上次使用的标记和popupView
私人标记mMarker;
私人PopupWindow mPopupWindow;
私人诠释mWidth;
私人诠释mHeight;
在我们的onMarkerClick:
@覆盖
公共布尔onMarkerClick(标记标记){
如果(mPopupWindow!= NULL){
mPopupWindow.dismiss();
}
查看popupView; //这里夸大我们的观点
PopupWindow popupWindow =新PopupWindow(popupView,ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); 显示显示= getActivity()getWindowManager()getDefaultDisplay()。
点大小=新点();
display.getSize(大小);
popupView.measure(size.x,size.y); mWidth = popupView.getMeasuredWidth();
mHeight = mPopupView.getMeasuredHeight();
mMarker =标记;
mPopupWindow = popupWindow; updatePopup(); 返回true;
}
updatePopup方式:
私人无效updatePopup(){
如果(mMarker = NULL&放大器;!&安培;!mPopupWindow = NULL){
//标记可见
如果(mMap.getProjection()。getVisibleRegion()。latLngBounds.contains(mMarker.getPosition())){
如果(!mPopupWindow.isShowing()){
mPopupWindow.showAtLocation(getView(),Gravity.NO_GRAVITY,0,0);
}
点P = mMap.getProjection()toScreenLocation(mMarker.getPosition());
mPopupWindow.update(p.x - mPopupWidth / 2,p.y - mPopupHeight + 100,-1,-1);
}其他{//标记外屏
mPopupWindow.dismiss();
}
}
}
我们需要CameraChangeListener:
mMap.setOnCameraChangeListener(本);@覆盖
公共无效onCameraChange(CameraPosition cameraPosition){
...
updatePopup();
}
I want to put a custom layout over a marker. So far I got this:
default_marker_info_window.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ViewFlipper
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/markerInfoContainer"
android:background="@android:color/holo_red_dark"
android:maxWidth="250dp">
</ViewFlipper>
<ImageView
android:layout_width="20dp"
android:layout_height="15dp"
android:background="@drawable/ic_down_arrow"
android:layout_gravity="center|top"/>
</LinearLayout>
default_marker_info_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tvTitulo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:gravity="center"/>
<TextView
android:id="@+id/tvCuerpo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="press me 1"
android:id="@+id/button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="press me 2"
android:id="@+id/button2"/>
</LinearLayout>
</LinearLayout>
MapActivity.java
public class MapActivity extends Activity implements GoogleMap.OnMapLongClickListener{
protected GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
setUpMapIfNeeded();
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
View mainView = getLayoutInflater().inflate(R.layout.default_marker_info_window, null);
ViewFlipper markerInfoContainer = (ViewFlipper)mainView.findViewById(R.id.markerInfoContainer);
View viewContainer = getLayoutInflater().inflate(R.layout.default_marker_info_layout, null);
TextView tvTitulo = (TextView) viewContainer.findViewById(R.id.tvTitulo);
TextView tvCuerpo = (TextView) viewContainer.findViewById(R.id.tvCuerpo);
tvTitulo.setText(marker.getTitle());
tvCuerpo.setVisibility(View.GONE);
markerInfoContainer.addView(viewContainer);
PopupWindow popupWindow = new PopupWindow(mainView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.showAtLocation(findViewById(R.id.map), Gravity.CENTER_HORIZONTAL, 0, 0); //map is the fragment on the activity layout where I put the map
return true;
}
});
...
}
...
}
But I have a couple of problems.
Problem 1: the popup shows over (3D) the marker (hidding it). I want to position it above it (2D), like this:
May be by help of x
and y
position of the marker and using them on popupWindow.showAtLocation
. But if you have other solutions, I will listen to them.
Problem 2: When I slide over the map, the layout keep on center of the screen, but I want to keep it over the marker.
Note: I can't use mMap.setInfoWindowAdapter
because I will lose the button's click listeners.
Define some variables to save last used marker and popupView
private Marker mMarker;
private PopupWindow mPopupWindow;
private int mWidth;
private int mHeight;
In our onMarkerClick:
@Override
public boolean onMarkerClick(Marker marker) {
if (mPopupWindow != null) {
mPopupWindow.dismiss();
}
View popupView;// inflate our view here
PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
popupView.measure(size.x, size.y);
mWidth = popupView.getMeasuredWidth();
mHeight = mPopupView.getMeasuredHeight();
mMarker = marker;
mPopupWindow = popupWindow;
updatePopup();
return true;
}
updatePopup method:
private void updatePopup() {
if (mMarker != null && mPopupWindow != null) {
// marker is visible
if (mMap.getProjection().getVisibleRegion().latLngBounds.contains(mMarker.getPosition())) {
if (!mPopupWindow.isShowing()) {
mPopupWindow.showAtLocation(getView(), Gravity.NO_GRAVITY, 0, 0);
}
Point p = mMap.getProjection().toScreenLocation(mMarker.getPosition());
mPopupWindow.update(p.x - mPopupWidth / 2, p.y - mPopupHeight + 100, -1, -1);
} else { // marker outside screen
mPopupWindow.dismiss();
}
}
}
We need CameraChangeListener:
mMap.setOnCameraChangeListener(this);
@Override
public void onCameraChange(CameraPosition cameraPosition) {
...
updatePopup();
}
这篇关于在标记的Android弹出或布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!