谷歌地图中的多个标记使用JSON

时间:2012-07-20 06:18:55

标签: android json google-maps

我必须使用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);
    }
}

}

1 个答案:

答案 0 :(得分:2)

您正在使用latitude而不是经度创建一个地理位置。

i

而不是:

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

因此所有的点都非常接近彼此。

我假设您使用'new GeoPoint((int)((latitude) * 1E6), (int)((longitude) * 1E6)) // + some variation based on i '来创建您所在位置附近的单独点,而在最终产品中您将使用ATM点数据库。我建议您创建一个应该检索ATM位置的函数,并且为了测试目的,要么返回固定(但是真实)的位置,要么在lat / lng周围随机化。 (这也是一种更好的编码实践。)

出于调试目的,请记录您正在创建的实际点,在创建之后。如果所有纬度/经度数看起来都相同,那么您就可以了解正在发生的事情。如果只记录两个点......这是一个不同的问题。 : - )