Question

I have got a mapview, but once I added a for loop I cannot seem to be able to center on the geopoint :(. Any Ideas?

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;
}}
Was it helpful?

Solution

you should use below code to make your point to center position in map

MapController controller = mapView.getController();
controller.setCenter(youtgeopoint);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top