如何根据标题,描述或纬度或经度在地图上查找标记

时间:2015-03-13 06:52:09

标签: android google-maps

我有一张地图,上面有各种标记。标记有标题和片段。 我想通过在地图上找到标记来显示信息窗口。方案如下: 我有一个网格,我有标题和片段。当用户点击一行时,标题和片段将传递给包含地图的片段 2.在Create方法上,我想在标记上显示包含标题和片段的信息窗口。

我的问题是如何通过s title and snippet and show it的信息窗口找到标记?

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

//如何在地图上找到具有标题和描述的标记以及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"/>

1 个答案:

答案 0 :(得分:1)

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

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

HashMap markerMap = new 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);

现在在你的搜索功能中,尝试找到带有搜索标题的地图

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
}