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





}

誰でも私を助けてもらえますか?

4

1 に答える 1

5

このコードはトリックを行う必要があります:

        lm.requestSingleUpdate(LocationManager.GPS_PROVIDER, this, null);
        Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        lat = loc.getLatitude();
        lng = loc.getLongitude();
于 2012-06-27T14:59:05.290 に答える