tags:

views:

163

answers:

2

Hi friends!

In my application I tried to search diff map location using diff lat and long. First time the application show the map but wen i change the lat long and try to invalidate the mapview using diff lat long, map is not refreshed.

Below is my code please have a look and suggest accordingly:

//Source code

protected void onCreate(Bundle icicle) {
        // TODO Auto-generated method stub
        super.onCreate(icicle);
        setContentView(R.layout.main);
        infoTextView = (TextView) findViewById(R.id.infoTextView);

        // Finding Current Location
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                1l, 1l, this);
    Location location = locationManager.getLastKnownLocation("gps");


    // mock location by hard-code if DDMS has not sent a fake loc to the emulator.
        if (location == null) {
            lat = 13.6972 * 1E6;
            lng = 100.5150 * 1E6;
    } else { // get real location if can retrieve the location sent by DDMS or GPS
            lat = location.getLatitude() * 1E6;
            lng = location.getLongitude() * 1E6;
        }
        setGPSLocation(lat, lng);
    }

//This is the function which I am calling with different lat and long

public void setGPSLocation(Double lati, Double longi)
    {
        lat = lati;
        lng = longi;
        System.out.println("Latitude :"+ lat +" Longitude :"+lng);

        // Prepare text being shown
        String tmpLoc = LOC_INFO_TEMPLATE;
        tmpLoc = tmpLoc.replace("lg", String.valueOf(lng));
        tmpLoc = tmpLoc.replace("lt", String.valueOf(lat));
        infoTextView.setText(tmpLoc);

        // Setup Zoom/Hide Buttons
        linearLayout = (LinearLayout) findViewById(R.id.zoomview);
        mapView = (MapView) findViewById(R.id.mapview);  
        mapView.invalidate();

        mapView.setBuiltInZoomControls(true);                           //new by me
        mapView.setSatellite(true); // Set satellite view
        mZoom = (ZoomControls) mapView.getZoomControls();
        linearLayout.addView(mZoom);

        // Setup Marker

        mapOverlays = mapView.getOverlays();
        drawable = this.getResources().getDrawable(R.drawable.marker);
        itemizedOverlay = new MyItemizedOverlay(drawable);
        GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue());
        OverlayItem overlayitem = new OverlayItem(point, "", "");
        itemizedOverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedOverlay);

        // Centralize Current Location
        myMapController = mapView.getController();
        myMapController.setZoom(DEFAULT_ZOOM_NUM);
        myMapController.animateTo(point);   



    }

Any suggest is truly appreciable.

A: 

Can you show the code for centerlizeCurrentLocation()? And can you include some output from the line "System.out.println("Latitude :"+ lat +" Longitude :"+lng);", so we can confirm that appropriate data is coming in?

Steve
A: 

You call setGPSLocation(Double lati, Double longi) method only once in onCreate(Bundle icicle) {...} That's why you can't refresh your map. You can use CountDownTimer to call your map every xxxx msec. Or create new button and call setGPSLocation(Double lati, Double longi) from it's onClick method.

using of CountDownTimer:

public class RefreshLocationCount extends CountDownTimer{
public RefreshLocationCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}

@Override
public void onFinish() {
    Toast.makeText(getApplicationContext(), "Counter::onFinish()!!",
              Toast.LENGTH_SHORT).show();
}

@Override
public void onTick(long millisUntilFinished) {
    setGPSLocation(Double lati, Double longi);
}

}


In onCreate():
{.....

RefreshLocationCount refreshLocationCounter = new RefreshLocationCount(10000,1000);
....
...
refreshLocationCounter.start();

}

Make your Location location - global variable, and use it in setGPSLocation()
lat = location.getLatitude() * 1E6; lng = location.getLongitude() * 1E6;

(don't pass parametrs to setGPSLocation(), just use location data there)

TSR1989