2

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

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

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

就像是:

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

4

2 回答 2

1

在这里,我发布了 Google MapView 的所有代码。它适用于 Fragment 和 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

于 2013-07-05T15:10:58.750 回答
1

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

于 2012-12-19T22:58:05.427 回答