gpt4 book ai didi

android - map v2 中带有自定义数据的自定义信息窗口适配器

转载 作者:IT王子 更新时间:2023-10-28 23:58:56 24 4
gpt4 key购买 nike

我想在 android 的 map v2 中制作自定义信息窗口适配器,如下所示。

enter image description here

我已经看到下面的链接,但没有得到更多。

1 , 2, 3 ,

下面是我的内容布局文件。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:id="@+id/infocontent_iv_image"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
/>
<RelativeLayout
android:id="@+id/infocontent_rl_middle"
android:layout_below="@id/infocontent_iv_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
>

</RelativeLayout>
<TextView
android:id="@+id/infocontent_tv_name"
android:layout_below="@id/infocontent_rl_middle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_margin="5dp"
/>
<TextView
android:id="@+id/infocontent_tv_type"
android:layout_below="@id/infocontent_tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#CCCCCC"
android:layout_margin="5dp"
/>
<TextView
android:id="@+id/infocontent_tv_desc"
android:layout_below="@id/infocontent_tv_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
/>
<TextView
android:id="@+id/infocontent_tv_addr"
android:layout_below="@id/infocontent_tv_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
/>
</RelativeLayout>

那么任何人都可以帮助我如何将数据设置到 infowindow 适配器中的所有 View ?

最佳答案

试试这个

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();

}
});

}
}

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

如果对你有帮助,请告诉我。

关于android - map v2 中带有自定义数据的自定义信息窗口适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15090148/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com