首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MapView MyLocationOverlay首次运行问题

MapView MyLocationOverlay首次运行问题
EN

Stack Overflow用户
提问于 2012-08-16 08:30:22
回答 2查看 491关注 0票数 0

我在应用程序中使用定位服务。如果我发送经度和纬度通过DDMS模拟器,蓝点动画到那里。但是,如果我关闭应用程序,重新启动,蓝点似乎不似乎。如果你看到蓝点,你必须发送新的经度和纬度。我想每次应用程序启动时都显示蓝点.

我能做些什么?

onCreate:

代码语言:javascript
复制
mc = mapView.getController();
        mapView.setBuiltInZoomControls(true);
        mc.setZoom(15);
        myLocOverlay = new MyLocationOverlay(this, mapView);
        mapView.getOverlays().add(myLocOverlay); 
        myLocOverlay.runOnFirstFix(new Runnable() {
            public void run() {
                mc.animateTo(myLocOverlay.getMyLocation());
                }
        });

        mapView.postInvalidate();

lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();       
        provider = lm.getBestProvider(criteria, false);

        Location location = lm.getLastKnownLocation(provider);

        if (location != null) {
            onLocationChanged(location);
            }

onLocationChanged(地点)

代码语言:javascript
复制
String PK="";

        double lat = location.getLatitude();
        double lng = location.getLongitude();
        Geocoder gc = new Geocoder(this,Locale.getDefault());
        try{

            List<Address> adresler = gc.getFromLocation(lat, lng, 1);
            StringBuilder sb = new StringBuilder();
            if(adresler.size()>0){
                Address adres = adresler.get(0);
                boolean sayi = false;
                for(int i=0;i<adres.getMaxAddressLineIndex();i++){
                    if(i!=2){
                        sb.append(adres.getAddressLine(i)).append("\n");
                    }
                    else{
                        String full = adres.getAddressLine(i).toString();
                        char chars[] = full.toCharArray();
                        for(int k=0;k<chars.length;k++){
                            sayi = Character.isDigit(chars[k]);
                            if(sayi){
                                PK=PK+chars[k];
                            }
                        }
                    }

                }   
                adresString = sb.toString();
                new gpsYerBilgisiAS(PK).execute();
            }
        }
            catch(IOException e){

            }
        }

onResume和onPause

代码语言:javascript
复制
@Override   
    protected void onResume() {  
        super.onResume();  
        lm.requestLocationUpdates(provider, 400, 1, this);      
        myLocOverlay.enableMyLocation();

    }
    @Override   
    protected void onPause() {    
        super.onPause();
        lm.removeUpdates(this); 
        myLocOverlay.disableMyLocation();
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-08-16 09:22:18

在模拟器中,每次调用onLocationChanged方法时都必须发送位置。在设备上,如果GPS是打开的,它就会自动完成。但我只会显示“蓝点”,当你的设备接收到的位置数据通过GPS或网络。

票数 1
EN

Stack Overflow用户

发布于 2012-08-16 08:45:13

让你的onResume()像这样:

代码语言:javascript
复制
protected void onResume() {
    ....
    myLocOverlay = new MyLocationOverlay(this, mapView);
    myLocOverlay.enableMyLocation();
    mapView.getOverlays().add(myLocOverlay); 
    myLocOverlay.runOnFirstFix(new Runnable() {
        public void run() {
            mc.animateTo(myLocOverlay.getMyLocation());
            }
    });
    ....
}

它会以你想要的方式运作。这是我目前在我自己的应用程序中使用的方式。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11983328

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档