1

我有一个地图视图,但是一旦我添加了一个 for 循环,我似乎无法以地理点为中心:(。有什么想法吗?

代码

public class MainActivity  extends MapActivity



{

public GeoPoint point;
TapControlledMapView mapView; // use the custom TapControlledMapView
List<Overlay> mapOverlays;
Drawable drawable;
SimpleItemizedOverlay itemizedOverlay;


 @Override
 public void onCreate(Bundle savedInstanceState)
 {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);


    mapView = (TapControlledMapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    mapView.setSatellite(false);
    // dismiss balloon upon single tap of MapView (iOS behavior) 
    mapView.setOnSingleTapListener(new OnSingleTapListener() {      
        public boolean onSingleTap(MotionEvent e) {
            itemizedOverlay.hideAllBalloons();
            return true;
        }
    });

    mapOverlays = mapView.getOverlays();

    // first overlay
    drawable = getResources().getDrawable(R.drawable.marker2);



    itemizedOverlay = new SimpleItemizedOverlay(drawable, mapView);
    // set iOS behavior attributes for overlay
    itemizedOverlay.setShowClose(false);
    itemizedOverlay.setShowDisclosure(true);
    itemizedOverlay.setSnapToCenter(false);

     InputStream is = null;
        try {
            is = getAssets().open("****.****");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int size = 0;
        try {
            size = is.available();
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }

        byte[] buffer = new byte[size];
        try {
            is.read(buffer);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            is.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String str = new String(buffer);
        String jsonString = null;
        try {
            jsonString = str;
        } catch (IllegalStateException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        // Create a JSON object that we can use from the String
        JSONObject json = null;
        try {
            json = new JSONObject(jsonString);
        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

                  try{

         JSONArray jsonArray = json.getJSONArray("groups");
         Log.e("log_tag", "Opening JSON Array 'Groups'");
            for(int i=0;i < jsonArray.length();i++){                        

                JSONObject jsonObject = jsonArray.getJSONObject(i);

                    String latitude =  jsonObject.getString("latitude");
                    String longitude =  jsonObject.getString("longitude");
                    String name =  jsonObject.getString("name");
                    String address =  jsonObject.getString("street");
                    double lat = Double.parseDouble(latitude);
                    double lng = Double.parseDouble(longitude);
                    double searchLat = Double.parseDouble("-31.4333");
                    double searchLon = Double.parseDouble("152.9000"); 

                 double distance;  

                Location locationA = new Location("point A");  

                locationA.setLatitude(searchLat);  
                locationA.setLongitude(searchLon);  

                Location locationB = new Location("point B");  

                locationB.setLatitude(lat);  
                locationB.setLongitude(lng);  

                distance = locationA.distanceTo(locationB); 

                    if (distance <= 1000 * 10) {

                         Log.e("log_tag", "ADDING GEOPOINT"+name); 

                          point = new GeoPoint(
                                 (int) (lat * 1E6), 
                                 (int) (lng * 1E6));
                        OverlayItem overlayItem = new OverlayItem(point, name, 
                                address);
                        itemizedOverlay.addOverlay(overlayItem);


                 }
                        }
                  }catch(JSONException e)        {
                     Log.e("log_tag", "Error parsing data "+e.toString());
                } 

                  itemizedOverlay.populateNow(); 

                  mapOverlays.add(itemizedOverlay);
                        if (savedInstanceState == null) {

                            final MapController mc = mapView.getController();


                            mc.animateTo(point);
                            mc.setZoom(12);

                        } else {

                            // example restoring focused state of overlays
                            int focused;
                            focused = savedInstanceState.getInt("focused_1", -1);
                            if (focused >= 0) {
                                itemizedOverlay.setFocus(itemizedOverlay.getItem(focused));
                            }

                        }}




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

1 回答 1

4

您应该使用以下代码将您的点指向地图中的中心位置

MapController controller = mapView.getController();
controller.setCenter(youtgeopoint);
于 2012-10-19T05:48:03.800 回答