3
我只是想知道是否有可能強制更新位置以獲得移動設備的位置,一旦我點擊菜單中的刷新項目。如何在單擊刷新菜單按鈕後強制更新位置?
下面是我在論壇中找到的代碼,我想添加一個新功能。
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class MapLocator extends MapActivity implements LocationListener {
/** Called when the activity is first created. */
private MapView mapView;
private LocationManager lm;
private MapController mc;
private double lat = 0;
private double lng = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
mapView = (MapView) this.findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
mc = mapView.getController();
mc.setZoom(14);
int FIVE_MINUTES = 5 /*Minutes*/ * 60 /*sec per min*/ * 1000 /*ms per sec*/;
lm = (LocationManager) this.getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, FIVE_MINUTES, 0, this);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, FIVE_MINUTES, 0, this);
Drawable drawable= this.getResources().getDrawable(R.drawable.loc_seven);
ListItimizedOverlay itemizedoverlay = new ListItimizedOverlay(drawable);
GeoPoint p = new GeoPoint(33000000,84000000);
OverlayItem overlayitem = new OverlayItem(p, "Hello from", "Tahiti");
itemizedoverlay.addOverlayItem(overlayitem);
mapView.getOverlays().add(itemizedoverlay);
}
@Override
protected boolean isRouteDisplayed()
{
return false;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_S)
{
mapView.setSatellite(!mapView.isSatellite());
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onLocationChanged(Location location)
{
Log.d("msg","1");
lat = location.getLatitude();
lng = location.getLongitude();
Toast.makeText(
getBaseContext(),
"Location change to : Latitude = " + lat + " Longitude = "
+ lng, Toast.LENGTH_SHORT).show();
GeoPoint p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
mc.animateTo(p);
mc.setCenter(p);
mc.setZoom(14);
Drawable drawable= this.getResources().getDrawable(R.drawable.loc_seven);
ListItimizedOverlay itemizedoverlay = new ListItimizedOverlay(drawable);
OverlayItem overlayitem = new OverlayItem(p, "", "");
itemizedoverlay= (ListItimizedOverlay) mapView.getOverlays().get(0);
itemizedoverlay.clear();
itemizedoverlay.addOverlayItem(overlayitem);
mapView.getOverlays().add(itemizedoverlay);
}
@Override
public void onProviderDisabled(String provider)
{
}
@Override
public void onProviderEnabled(String provider)
{
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
//Menu with 2 items Refresh and exit
public boolean onCreateOptionsMenu(Menu menu2) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.menumap, menu2);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh:
/*
---------------------------------------------------------------------------------------
code to add in order to refrech the location and position the mobile
on the map using the drawable in the itemizedoverlay. I have tried this :
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
and nothing is changed i still see the old position on the map
and I have tested the app on a sony Ericsson Xperia x10.
I am using google API 10 if this might help.
------------------------------------------------------------------------------------------
*/
return true;
case R.id.exit:
finish();
return true;
}
return false;}
}
任何人都可以幫助我嗎?
它沒有工作。我是否需要使用新的LocationManager? – user1485844
這些只是我實施這個時使用的變量。我將更新爲你的 –
好的,謝謝你會等待 – user1485844