2012-12-19 76 views
2

我爲android使用了新功能addMarker googlemaps api。使用Google Maps API v2添加標記功能android

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

的偉大工程,但我的問題是,我怎麼能放置標記指示搜索字符串,而不是指示經度的參數和緯度?

類似:

「聖女盧漢42,塞維利亞,西班牙」

回答

1

你必須找到與「搜索字符串」相關聯的緯度和經度,地理編碼使用。 Android中有一個Geocoder類,還有其他各種可供您使用的地理編碼Web服務。

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

相關問題