2012-07-20 40 views
1

我必須使用JSON解析製作多標記谷歌地圖。任何人在幫助它的代碼。這樣我就可以獲得一個想法。我所做的活動並不完全符合我的要求。 這是我的活動使用JSON的谷歌地圖中的多個標記

public class MapsActivity extends MapActivity{ 
private MapController mapControll; 
private GeoPoint geoPoint=null; 
private MapView mapview; 
private MyItemizedOverlay userPicOverlay; 
private MyItemizedOverlay nearPicOverlay; 
private Drawable userPic,atmPic; 
private OverlayItem nearatms[] = new OverlayItem[50]; 
public static Context context; 
public double latitude; 
public double longitude; 


private List<String> lat = new ArrayList<String>(); 
private List<String> lon = new ArrayList<String>(); 
private String[] get_lati; 
private String[] get_long; 

@Override 
protected void onCreate(Bundle icicle) { 
    // TODO Auto-generated method stub 
    super.onCreate(icicle); 
    context = getApplicationContext(); 
    setContentView(R.layout.map); 
    lat.clear(); 
    lon.clear(); 
    Intent in = getIntent(); 
    if(in!=null){ 
    get_lati = in.getStringArrayExtra("LAT_KEY"); 
    get_long = in.getStringArrayExtra("LONG_KEY"); 
    System.out.println("lat===="+get_lati.length+" lon==="+get_long.length); 
    } 
    for(int i = 0;i<get_lati.length;i++){ 
     String lt = null; 
     String ln = null; 
     lt =get_lati[i]; 
     ln =get_long[i]; 

     System.out.println("lt=="+lt+"==ln=="+ln); 
    } 
    showMap(); 
} 

public void showMap() { 
    // TODO Auto-generated method stub 

    try { 
     double lat_dbl = Double.parseDouble(get_lati[0]); 
     double lon_dbl = Double.parseDouble(get_long[0]); 
     geoPoint = new GeoPoint((int)(lat_dbl * 1E6),(int)(lon_dbl * 1E6));  
     mapview = (MapView)findViewById(R.id.mapView); 
     mapControll= mapview.getController(); 
     mapview.setBuiltInZoomControls(true); 
     mapview.setStreetView(true); 
     mapControll.setZoom(16); 
     mapControll.animateTo(geoPoint); 

     userPic = this.getResources().getDrawable(R.drawable.pin); 
     userPicOverlay = new MyItemizedOverlay(userPic); 
     OverlayItem overlayItem = new OverlayItem(geoPoint, "I'm Here!!!", null); 
     userPicOverlay.addOverlay(overlayItem); 
     mapview.getOverlays().add(userPicOverlay); 


     atmPic = this.getResources().getDrawable(R.drawable.pin); 
     nearPicOverlay = new MyItemizedOverlay(atmPic); 
     for (int i = 0; i < 5; i++) { 
      nearatms[i] = new OverlayItem(new GeoPoint((int)((latitude) * 1E6), i),"Name", null);//just check the brackets i just made change here so.... 
      nearPicOverlay.addOverlay(nearatms[i]); 
     } 
     mapview.getOverlays().add(nearPicOverlay); 
     //Added symbols will be displayed when map is redrawn so force redraw now 
     mapview.postInvalidate(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

@Override 
protected boolean isRouteDisplayed() { 
    // TODO Auto-generated method stub 
    return false; 
} 



public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> { 

    private ArrayList<OverlayItem> myOverlays ; 

    public MyItemizedOverlay(Drawable defaultMarker) { 
     super(boundCenterBottom(defaultMarker)); 
     myOverlays = new ArrayList<OverlayItem>(); 
     populate(); 
    } 

    public void addOverlay(OverlayItem overlay){ 
     myOverlays.add(overlay); 
     populate(); 
    } 

    @Override 
    protected OverlayItem createItem(int i) { 
     return myOverlays.get(i); 
    } 

    // Removes overlay item i 
    public void removeItem(int i){ 
     myOverlays.remove(i); 
     populate(); 
    } 

    // Returns present number of items in list 
    @Override 
    public int size() { 
     return myOverlays.size(); 
    } 


    public void addOverlayItem(OverlayItem overlayItem) { 
     myOverlays.add(overlayItem); 
     populate(); 
    } 


    public void addOverlayItem(int lat, int lon, String title) { 
     try { 
      GeoPoint point = new GeoPoint(lat, lon); 
      OverlayItem overlayItem = new OverlayItem(point, title, null); 
      addOverlayItem(overlayItem);  
     } catch (Exception e) { 
      // TODO: handle exception 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    protected boolean onTap(int index) { 
     // TODO Auto-generated method stub 
     String title = myOverlays.get(index).getTitle(); 
     //Toast.makeText(MapActivity.this, title, Toast.LENGTH_LONG).show(); 
     return super.onTap(index); 
    } 
} 

}

+0

你的問題是什麼? – Marcelo 2012-07-20 07:41:10

+0

我的代碼是在後臺獲取緯度日誌,但它只顯示地圖上的兩個針腳。 – 2012-07-20 07:49:39

回答

2

您正在創建與latitude一個GeoPoint但 'i',而不是經度。

new GeoPoint((int)((latitude) * 1E6), i) 

代替:

new GeoPoint((int)((latitude) * 1E6), (int)((longitude) * 1E6)) // + some variation based on i 

因此所有的點非常接近下降到對方。

我假設您使用'i'爲了在您的位置附近創建單獨的點,並且在最終產品中您將使用ATM點數據庫。我建議你創建一個應該檢索自動取款機位置的函數,並且爲了測試的目的,要麼返回固定的(但是真實的)位置,要麼圍繞你的經緯度隨機化。 (這也是一個更好的編碼實踐。)

爲了調試,登錄實際點要創建,後您創建它們。如果所有的緯度/經度數字看起來都一樣,你就會知道發生了什麼。如果只記錄兩點......這是一個不同的問題。 :-)