单击googlemap标记时从底部的android弹出窗口

时间:2017-01-01 16:56:47

标签: android popup window marker

我是Android编程新手。当用户点击谷歌地图标记时,需要有关如何从屏幕底部制作弹出窗口的帮助。像Zillow应用程序一样。我搜索了一段时间,似乎coordinatorlayout可以做到,不确定是否有更好/更简单的方法来做到这一点。另外看了谷歌地图信息窗口,它总是显示在标记之上,不确定是否可以从屏幕底部显示信息窗口。提前谢谢!

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:0)

对于您所问的问题,这些代码对我有效@HappyFish。 MainActivity的代码:

  public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    Marker marker;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(final GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera

    LatLng dehiwala= new LatLng(6.848174, 79.865041);
        LatLng sydney= new LatLng(-33.852766, 151.183472);

        mMap.addMarker(new MarkerOptions().position(dehiwala));
        mMap.addMarker(new MarkerOptions().position(sydney));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));


     googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                openPopUpWindow();



             return true;
            }



        });
    }

    private void openPopUpWindow() {
       Intent popupwindow = new Intent (MapsActivity.this,PopUpWindow.class);
       startActivity(popupwindow);
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/////Add markers from firestore





}

activity_maps.xml的代码:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapsActivity" />    
    
    
    

PopUpWindow的代码:

public class PopUpWindow extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pop_up_window);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width =dm.widthPixels;
        int hight=dm.heightPixels;
      //  getWindow().setLayout((int)(width*.7),(int)(hight*.5));
        getWindow().setLayout((int)(width*.8),(int)(hight*.4));
        WindowManager.LayoutParams params = getWindow().getAttributes();
      params.gravity = Gravity.CENTER;
       // params.gravity = Gravity.BOTTOM;
        params.x=10;
        params.y=12;
        getWindow().setAttributes(params);

    }

}                     

activity_pop_up_window.xml的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".PopUpWindow"
    android:background="#fff">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pop Up Window"
        android:textColor="#fff"
        android:textSize="40sp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"/>


</RelativeLayout>

styles.xml的代码:

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="AppTheme.popMe">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowCloseOnTouchOutside">true</item>
    </style>
</resources>