Android地图定位的问题

堆堆 发布于 2012/02/03 11:28
阅读 4K+
收藏 1
最近在做基于Google Api的个人地图,但是定位功能总是无法实现,后来在网上找 了一个代码,可以定位到我家了,但是我去另外一个市再找开这个软件时,发现定位还是我家,代码如下:
只有2个Activity,第一个是欢迎界面,不说了,主Activity代码如下(图片是模拟器的显示效果):
……
public class MapsActivity extends MapActivity {
        private MapView mapView;
        private MapController mapController;
        private GeoPoint p;
        private String provider;
        private LocationManager locationManager;
        MyLocationOverlay mlo;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        /**
         * 初始化位置服务
         * 初始化locationManager,并且找到最合适的LocationProvider
         */
        locationManager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
final Location location=getCurrentLocation(locationManager);
/*----初始化地图显示和地图控制变量----*/
        mapView=(MapView)findViewById(R.id.mapview);
        mapController=mapView.getController();
mlo=new MyLocationOverlay(this, mapView);
        mlo.enableMyLocation();
        mlo.runOnFirstFix(new Runnable() {
                        @Override
                        public void run() {
                                mapController.animateTo(mlo.getMyLocation());
                                mapController.setZoom(16);
                        }
                });
        
        mapView.setClickable(true);
        mapView.setEnabled(true);
        mapView.setSatellite(false);
        mapView.setTraffic(false);
        mapView.setStreetView(false);
        mapView.setBuiltInZoomControls(true);  //设置缩放按钮可见
        
        mapController.setZoom(16);
        mapView.invalidate();
    }
/**
     * @return the current location
     */
    private Location getCurrentLocation(LocationManager locationManager) {
         Location location = locationManager.getLastKnownLocation("gps");
        if (null != location) { return location; }
        // getLastKnownLocation returns null if loc provider is not enabled
        location = new Location("gps");
        location.setLatitude(39);
        location.setLongitude(116);
        return location;
    }
/*---------------添加Menu控件------------------*/
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                menu.add(0, 1, 1, R.string.menu_help).setIcon(R.drawable.icon_help);
                menu.add(0, 2, 2, R.string.menu_exit).setIcon(R.drawable.icon_exit);
                return super.onCreateOptionsMenu(menu);
        }
        /*----------------Menu控件监听响应----------------------*/
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
                switch(item.getItemId()){
                case 1: startActivity(new Intent(MapsActivity.this,aboutActivity.class));
                        break;
                case 2: this.finish();
                                break;
                }
                return super.onOptionsItemSelected(item);
        }
        
        @Override
        protected boolean isRouteDisplayed() {
                // TODO Auto-generated method stub
                return false;
        }

        @Override
        protected void onDestroy() {
                //locationManager.removeUpdates(locationListener);
                super.onDestroy();
        }
        
}
发现里面定位用的是Location location = locationManager.getLastKnownLocation("gps");
是不是这个是获取上次定位的位置,而不是我要的实时定位啊?????????
在线等 ,急!
加载中
0
cxlin007
cxlin007
Location location = locationManager.getLastKnownLocation("gps");这句是获取gps上次存储的gps信息,所以还是你家。你应该开启gps 的监听器不断的定位, locationManager.requestLocationUpdates
堆堆
那我想实时定位怎么实现呢??
0
cxlin007
cxlin007
 locationManager = (LocationManager) mContext .getSystemService(Context.LOCATION_SERVICE);

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, GpsLocationListerner)这个方法中
GpsLocationListerner为Gps监听程序。有位置更新时会回掉里面的方法
cxlin007
cxlin007
@堆堆 : Location location = locationManager.getLastKnownLocation("gps"); 这句要去掉,不然每次都会获得,然后你的执行就结束了
堆堆
这个方法我试过,也无法实时定位,Geo设为北京定位还是原来的
返回顶部
顶部