使用Google Maps API v2 android添加标记功能

时间:2012-12-19 22:57:08

标签: android google-maps-markers android-maps

我正在使用新功能addMarker googlemaps api for android。

map.addMarker(new MarkerOptions()
        .position(new LatLng(40.417325, 40.417325))
        .title("Hello!"));

工作得很好,但我的问题是,我如何放置标记指示搜索字符串而不是指示经度和纬度的参数?

类似的东西:

“virgen de lujan 42,塞维利亚,西班牙”

2 个答案:

答案 0 :(得分:1)

您必须使用地理编码找到与该“搜索字符串”相关联的纬度和经度。 Android中有一个Geocoder类,您可以使用各种其他地理编码Web服务。

答案 1 :(得分:1)

这里我发布了Google MapView的所有代码。它适用于Fragement和Activity。此代码是为Api V2编写的

public class GeneralGoogleMapviewFragment extends Fragment {

private GoogleMap mMap;
private static final LatLng SYDNEY = new LatLng(31.0162, 71.6926);
private static final LatLng MOUNTAIN_VIEW = new LatLng(32.9043, 110.4677);
protected static final String TAG = "GeneralGoogleMapview";
private List<MyDataHolder> list;
private JSONObject jsonObj;
Intent intent;

MapView m;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle bundle = getArguments();
    String extra = bundle.getString("json");
    if (extra != null) {
        try {
            jsonObj = new JSONObject(extra);
            Logger.d(TAG, "Extra value is :" + extra.toString());
            list = Parser.parse(jsonObj);

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    try {
        MapsInitializer.initialize(this.getActivity());
    } catch (GooglePlayServicesNotAvailableException e) {
        e.printStackTrace();
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.mapview, container, false);
    m = (MapView) v.findViewById(R.id.mapView);
    m.onCreate(savedInstanceState);
    loadMap(list);
    return v;

}

@Override
public void onResume() {
    super.onResume();
    m.onResume();
    // setupMap();

}

@Override
public void onPause() {

    super.onPause();

    m.onPause();

}

@Override
public void onDestroy() {

    super.onDestroy();

    m.onDestroy();

}

@Override
public void onLowMemory() {

    super.onLowMemory();

    m.onLowMemory();

}

public void loadMap(List<MyDataHolder> markerlist) {

    mMap = m.getMap();
    MyDataHolder todaymarker;
    if (markerlist != null) {
        Logger.d(TAG, " No of Markers" + markerlist.size());
        for (int i = 0; i < markerlist.size(); i++) {
            todaymarker = markerlist.get(i);
            // Logger.d(
            // TAG,
            // " Marker Detail : " + " Latitude "
            // + todaymarker.getLocation().getLatitude() + " Longitude"
            // + todaymarker.getLocation().getLongitude() + " title "
            // + todaymarker.getEvents().getTitle());
            mMap.addMarker(new MarkerOptions()
                    .position(
                            new LatLng(todaymarker.getLoc_latitude(),
                                    todaymarker.getLoc_longitude()))
                    .title(todaymarker.getName())
                    .icon(BitmapDescriptorFactory
                            .fromResource(R.drawable.marker_icon)));
        }
    }

    mMap.setOnMarkerClickListener(new OnMarkerClickListener() {

        @Override
        public boolean onMarkerClick(Marker marker) {
            MyDataHolder todaymarker, todaymarker2 = null;
            if (list != null) {
                Logger.d(TAG, " No of Markers" + list.size());
                for (int i = 0; i < list.size(); i++) {
                    todaymarker = list.get(i);
                    if (todaymarker.getName().equals(marker.getTitle()))
                        todaymarker2 = todaymarker;
                }
            }

            Intent intent = new Intent();
            intent.setClass(getActivity(), SpecificPOI_Activity.class);
            intent.putExtra("poi", todaymarker2);
            intent.putExtra("type", "sobre");
            startActivity(intent);
            return true;
        }
    });

    // mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new
    // LatLng(31.50099,74.324741), 16.0f));
    /*
     * mMap.moveCamera(CameraUpdateFactory.newLatLngZoom( new
     * LatLng(Util.currentLocation.getLatitude(),
     * Util.currentLocation.getLongitude()), 16.0f));
     */
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
            -16.677224, -49.267698), 16.0f));
    mMap.setMyLocationEnabled(true);

}

  }

我也在发布xml文件 的 mapview.xml