如何有可能在位置更新的频率(每秒,几分钟等)之间进行选择?

时间:2012-07-20 14:11:38

标签: android geolocation gps locationlistener

您好我只想知道如何通过单击菜单项来修改位置更新的频率 我试过这个,但似乎我在这里做的不好(下面添加的代码),因为我没有在所选频率的位置更新。 我只是想知道是否有可能改变频率,以及最好的实施方式。 我希望能够刷新位置:通过单击子菜单项只能刷新一次,每秒或每2分钟。 项目选择最终

任何人都可以告诉我,如果我正在做的事情是对的,并且不让我知道我该怎么办? 提前致谢

以下是代码:

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.content.Intent;
import android.content.Intent;
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 LocationManager lm;



@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);


    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)
 {
     if(provider.equals("LocationManager.GPS_PROVIDER")){
         Toast.makeText(MapLocator.this, "GPS is off", Toast.LENGTH_LONG).show();

         startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
         }

     }

 @Override
 public void onProviderEnabled(String provider)
 {

    Log.v("main", "Enabled");
        Toast.makeText(MapLocator.this, "GPS Enabled", Toast.LENGTH_SHORT).show();
 }

 @Override
 public void onStatusChanged(String provider, int status, Bundle extras)
 {
 }

 @Override
 public void onPause() {
     super.onPause();
     lm.removeUpdates(this);
     }

  //Méthode qui se déclenchera lorsque vous appuierez sur le bouton menu du téléphone
    public boolean onCreateOptionsMenu(Menu menu2) {

        //Création d'un MenuInflater qui va permettre d'instancier un Menu XML en un objet Menu
        MenuInflater inflater = getMenuInflater();
        //Instanciation du menu XML spécifier en un objet Menu
        inflater.inflate(R.layout.menumap, menu2);

        return true;
     }


 public boolean onOptionsItemSelected(MenuItem item) {
         //On regarde quel item a été cliqué grâce à son id et on déclenche une action

          switch (item.getItemId()) {
            case R.id.refresh:
                return true;

            case R.id.refreshonce:

                 this.lm.requestSingleUpdate(LocationManager.GPS_PROVIDER, this, null);
                 Toast.makeText(MapLocator.this, "refreshnow", Toast.LENGTH_LONG).show();
                return true;

            case R.id.refresheachsec:

                this.lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,  0, 0, this);
                 Toast.makeText(MapLocator.this, "refresh2", Toast.LENGTH_LONG).show();
                return true;   

            case R.id.refresheach2min:

                this.lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,  2*60*1000, 0, this);
                 Toast.makeText(MapLocator.this, "refresh2", Toast.LENGTH_LONG).show();
                return true;        


            case R.id.refresheach5min:

                this.lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,  5*60*1000, 0, this);
                 Toast.makeText(MapLocator.this, "refresh5", Toast.LENGTH_LONG).show();
                return true;   

           case R.id.quitter:

               finish();
               return true;
         }

1 个答案:

答案 0 :(得分:0)

我建议在使用新时间请求更新之前使用LocationManager.removeUpdates()。另请注意,在JellyBean(4.1)中,更新之间的最短时间不再仅仅是一个提示。

请参阅:Location Manager in Android

“在Jellybean之前,minTime参数只是一个提示,并且一些位置提供程序实现忽略了它。从Jellybean开始,Android兼容设备必须同时观察minTime和minDistance参数。”