gpt4 book ai didi

android - 如何根据标题、描述或纬度或经度在 map 上查找标记

转载 作者:行者123 更新时间:2023-11-30 02:17:45 25 4
gpt4 key购买 nike

我有一张 map ,上面有各种标记。标记有标题和 fragment 。我想通过在 map 上找到标记来显示信息窗口。场景如下:1. 我有一个网格,其中有标题和摘要。当用户点击一行时,标题和 fragment 被传递到包含 map 的 fragment 。
2. 在 Create 方法上,然后我想在具有标题和 fragment 的标记上显示信息窗口。

我的问题是如何通过它的标题和 fragment 找到标记并显示它的信息窗口?

public class MapFragment extends Fragment {

private MapView map;
private static final LatLng MyRegion = new LatLng(-20, 20);

public MapFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = this.getArguments();

//String placeName = bundle.toString(key, defaultValue);
//String title = bundle.toString(key, defaultValue);

VerifyIfMarkerExistsAndShowDetails(title, description)
}

private void VerifyIfMarkerExistsAndShowDetails(String title, String Description)
{
GoogleMap gMap = map.getMap();

//如何在 map 上找到具有标题和描述以及showInfo()的标记

}

private void CreateMarkers(){

GoogleMap gMap = map.getMap();

gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

gMap.addMarker(new MarkerOptions().title("Title1")
.snippet("Description1")
.position(new LatLng( -20.3, 19.1)));

gMap.addMarker(new MarkerOptions().title("Title2")
.snippet("Description2")
.position(new LatLng(-20.1, 19.2)));
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);

if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity().getApplicationContext()) == ConnectionResult.SUCCESS) {
//Initialze mapview
MapsInitializer.initialize(getActivity());
map = (MapView) view.findViewById(R.id.mapView);
map.onCreate(savedInstanceState);
CreateMarkers();

} else {
Toast.makeText(getActivity(), "Please install google play services", Toast.LENGTH_LONG).show();
}
return view;
}

@Override
public void onResume() {
super.onResume();
map.onResume();
}

@Override
public void onPause() {
super.onPause();
map.onPause();
}

@Override
public void onDestroy() {
super.onDestroy();
map.onDestroy();
}

@Override
public void onLowMemory() {
super.onLowMemory();
map.onLowMemory();
}

xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

最佳答案

需要在顶部导入java.util.HashMap,然后实例化一个Hashmap对象,

import java.util.HashMap;
import com.google.android.gms.maps.model.Marker;

HashMap markerMap = new HashMap();

每次创建新标记时,将其添加到 HashMap 中,

Marker marker = map.addMarker(new MarkerOptions()
.position(new LatLng(37.7750, 122.4183))
.title("San Francisco")
.snippet("Population: 776733"));

markerMap.put("San Francisco", marker);

现在在您的搜索功能中,尝试找到带有搜索标题的 map ,

private void VerifyIfMarkerExistsAndShowDetails(String title, String Description)
{
Marker marker = (Marker)markerMap.get(title);
if(marker==null)
{
//* show error marker does not exist
}
//* else do something, maybe center the map there
}

关于android - 如何根据标题、描述或纬度或经度在 map 上查找标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29026442/

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