Android MapView不以点为中心

时间:2012-10-19 05:44:22

标签: android json google-maps geopoints

我有一个mapview,但是一旦我添加了for循环,我似乎无法集中在geopoint上:(。任何想法?

CODE

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;
}}

1 个答案:

答案 0 :(得分:4)

你应该使用下面的代码使你的地点位于地图的中心位置

MapController controller = mapView.getController();
controller.setCenter(youtgeopoint);