【问题标题】:Custom info window with address带有地址的自定义信息窗口
【发布时间】:2016-09-16 11:01:28
【问题描述】:

我试图在一个漂亮的小盒子中显示用户的位置,如下所示。目前我的标记上只有一个“.title()”,所以我不确定如何创建一个包含以下信息的蓝色透明框:

http://s32.postimg.org/v6z9yaamd/Untitled.png

我已经有了用户当前的经度和纬度,我猜你可以通过谷歌获取用户地址。如果您知道如何设置信息窗口的样式和/或如何显示地址,我们将不胜感激。

【问题讨论】:

    标签: java android android-studio google-maps-markers infowindow


    【解决方案1】:

    试试这个

    windowlayout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/tv_lat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <TextView
            android:id="@+id/tv_lng"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
    </LinearLayout>
    

    MainActivity.java

        public class MainActivity extends FragmentActivity {
    
    
    GoogleMap googleMap;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        // Getting reference to the SupportMapFragment of activity_main.xml
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    
        // Getting GoogleMap object from the fragment
        googleMap = mapFragment.getMap();
    
        // Setting a custom info window adapter for the google map
        googleMap.setInfoWindowAdapter(new InfoWindowAdapter() {
    
            // Use default InfoWindow frame
            @Override
            public View getInfoWindow(Marker arg0) {
                return null;
            }
    
            // Defines the contents of the InfoWindow
            @Override
            public View getInfoContents(Marker arg0) {
    
                // Getting view from the layout file info_window_layout
                View v = getLayoutInflater().inflate(R.layout.windowlayout, null);
    
                // Getting the position from the marker
                LatLng latLng = arg0.getPosition();
    
                // Getting reference to the TextView to set latitude
                TextView tvLat = (TextView) v.findViewById(R.id.tv_lat);
    
                // Getting reference to the TextView to set longitude
                TextView tvLng = (TextView) v.findViewById(R.id.tv_lng);
    
                // Setting the latitude
                tvLat.setText("Latitude:" + latLng.latitude);
    
                // Setting the longitude
                tvLng.setText("Longitude:"+ latLng.longitude);
    
                // Returning the view containing InfoWindow contents
                return v;
    
            }
        });
    
        // Adding and showing marker while touching the GoogleMap
        googleMap.setOnMapClickListener(new OnMapClickListener() {
    
            @Override
            public void onMapClick(LatLng arg0) {
                // Clears any existing markers from the GoogleMap
                googleMap.clear();
    
                // Creating an instance of MarkerOptions to set position
                MarkerOptions markerOptions = new MarkerOptions();
    
                // Setting position on the MarkerOptions
                markerOptions.position(arg0);
    
                // Animating to the currently touched position
                googleMap.animateCamera(CameraUpdateFactory.newLatLng(arg0));
    
                // Adding marker on the GoogleMap
                Marker marker = googleMap.addMarker(markerOptions);
    
                // Showing InfoWindow on the GoogleMap
                marker.showInfoWindow();
    
            }
        });
    
    }
    }
    

    通过这种方式,您可以创建自定义布局并实现您想要的方式。

    【讨论】:

      【解决方案2】:

      这里你更新了 infowindow 的代码

      public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
      
          GoogleMap Map;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
              // Getting reference to the SupportMapFragment of activity_main.xml
              SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
      
              // Getting GoogleMap object from the fragment
              mapFragment.getMapAsync(this);
      
              // Setting a custom info window adapter for the google map
      
          }
      
          @Override
          public boolean onCreateOptionsMenu(Menu menu) {
              // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.activity_main, menu);
              return true;
          }
      
          @Override
          public void onMapReady(final GoogleMap googleMap) {
              this.Map = googleMap;
              Map.setInfoWindowAdapter(new InfoWindowAdapter() {
      
                  // Use default InfoWindow frame
                  @Override
                  public View getInfoWindow(Marker arg0) {
                      return null;
                  }
      
                  // Defines the contents of the InfoWindow
                  @Override
                  public View getInfoContents(Marker arg0) {
      
                      // Getting view from the layout file info_window_layout
                      View v = getLayoutInflater().inflate(R.layout.info_window_layout, null);
      
                      // Getting the position from the marker
                      LatLng latLng = arg0.getPosition();
      
                      // Getting reference to the TextView to set latitude
                      TextView tvLat = (TextView) v.findViewById(R.id.tv_lat);
      
                      // Getting reference to the TextView to set longitude
                      TextView tvLng = (TextView) v.findViewById(R.id.tv_lng);
      
                      // Setting the latitude
                      tvLat.setText("Latitude:" + latLng.latitude);
      
                      // Setting the longitude
                      tvLng.setText("Longitude:" + latLng.longitude);
      
                      // Returning the view containing InfoWindow contents
                      return v;
      
                  }
      
              });
      
      
              // Adding and showing marker while touching the GoogleMap
              Map.setOnMapClickListener(new OnMapClickListener() {
      
                  @Override
                  public void onMapClick(LatLng arg0) {
                      // Clears any existing markers from the GoogleMap
                      Map.clear();
      
                      // Creating an instance of MarkerOptions to set position
                      MarkerOptions markerOptions = new MarkerOptions();
      
                      // Setting position on the MarkerOptions
                      markerOptions.position(arg0);
      
                      // Animating to the currently touched position
                      Map.animateCamera(CameraUpdateFactory.newLatLng(arg0));
      
                      // Adding marker on the GoogleMap
                      Marker marker = Map.addMarker(markerOptions);
      
                      // Showing InfoWindow on the GoogleMap
                      marker.showInfoWindow();
      
                  }
              });
      
      
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-07-17
        • 2013-02-11
        • 1970-01-01
        • 2013-05-20
        • 2018-04-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多