当我尝试获取我的位置时始终返回null

时间:2011-12-07 20:11:43

标签: android latitude-longitude

我正在尝试获取mi位置(通过gps或wifi相同),并且永远不会获得,如果我调试在gotLocation()方法中有一个bucle。我总是使用相同的结果尝试很多方法:这是我的课程

public void onCreate(Bundle savedInstanceState) {     
    super.onCreate(savedInstanceState);   
    setContentView(R.layout.editarlugar);   

    TextView eNombre = (TextView) findViewById(R.id.eNombre);
    TextView eDesc = (TextView) findViewById(R.id.eDesc);
    ImageView eFoto = (ImageView) findViewById(R.id.iFoto);
    eMapa = (MapView) findViewById(R.id.editarMapa);





     /********************
     * Obtengo los datos del item, con intent procedente 
     * de otro activity   y tambien digo que tipo de accion es: Crear o Editar     
     *********************/

      final Bundle extras = getIntent().getExtras();
       String nombreClick = null;

      if(extras!=null){
                 nombreClick = extras.getString("nombre");
                 estado = 2;//Estoy editando
                 Editando(nombreClick,eNombre,eDesc,eFoto);

             }  


    /********************************************************************************** 
     * Instanciamos el mapa de google
     * Le ponemos controles de zoom
     **********************************************************************************/

   eMapa.setBuiltInZoomControls(true);
   eMapa.displayZoomControls(true);
   eMapa.setClickable(true);
   controladorMapa = eMapa.getController();

   //Nos geolizaliza y setea el geopunto yo
   encuentrame();
   OverlayItem [] listaPuntos = {
           new OverlayItem(yo, "Estas aqui", "Marcado!"),
       };
   dibujaPuntos();




}


private void encuentrame() {
            miPosicion.obtenLocalizacion(this, locationResult);
        }

public LocationResult locationResult = new LocationResult(){
            @Override
            public void gotLocation(final Location location){

                    if( location == null ){
                        //No hagas nada
                    }else{
                        latitud = location.getLatitude();
                        longitud = location.getLongitude();

                         yo = new GeoPoint((int)(latitud * 1E6), (int)(longitud * 1E6));
                    }

                };
        };      



public void dibujaPuntos(){ 
    misPuntos = eMapa.getOverlays();    
    imagen = this.getResources().getDrawable(R.drawable.yo); 
    punto = new MyItemizedOverlay(imagen); 
    misPuntos.add(punto);

}

/************************
 * Metodo obligatorio
 * 
 **********************/

@Override
protected boolean isRouteDisplayed() {
    return false;
}





}

这是位置类

//脚本copiada de stackoverlow obtains from here

public boolean obtenLocalizacion(Context context, LocationResult result)
{
    //Uso LocationResult para devolver la localizacion a otras clases
    locationResult=result;
    if(lm==null)
        lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

    //Excepciones no permitidas
    try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
    try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}

    //No se inician listeners hasta que no esta encendido ningun provider
    if(!gps_enabled && !network_enabled)
        return false;

    if(gps_enabled)
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, porGPS);
    if(network_enabled)
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, porWifi);
    timer1=new Timer();
    timer1.schedule(new obtenUltimaLoc(), 20000);
    return true;
}

LocationListener porGPS = new LocationListener() {
    public void onLocationChanged(Location location) {
        timer1.cancel();
        locationResult.gotLocation(location);
        lm.removeUpdates(this);
        lm.removeUpdates(porWifi);
    }
    public void onProviderDisabled(String provider) {}
    public void onProviderEnabled(String provider) {}
    public void onStatusChanged(String provider, int status, Bundle extras) {}
};

LocationListener porWifi = new LocationListener() {
    public void onLocationChanged(Location location) {
        timer1.cancel();
        locationResult.gotLocation(location);
        lm.removeUpdates(this);
        lm.removeUpdates(porGPS);
    }
    public void onProviderDisabled(String provider) {}
    public void onProviderEnabled(String provider) {}
    public void onStatusChanged(String provider, int status, Bundle extras) {}
};

class obtenUltimaLoc extends TimerTask {
    @Override
    public void run() {
         lm.removeUpdates(porGPS);
         lm.removeUpdates(porWifi);

         Location net_loc=null, gps_loc=null;
         if(gps_enabled)
             gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
         if(network_enabled)
             net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

         if(gps_loc!=null && net_loc!=null){
             if(gps_loc.getTime()>net_loc.getTime())
                 locationResult.gotLocation(gps_loc);
             else
                 locationResult.gotLocation(net_loc);
             return;
         }

         if(gps_loc!=null){
             locationResult.gotLocation(gps_loc);
             return;
         }
         if(net_loc!=null){
             locationResult.gotLocation(net_loc);
             return;
         }
         locationResult.gotLocation(null);
    }
}

public static abstract class LocationResult{
    public abstract void gotLocation(Location location);
}

}

因此,当我尝试绘制mi olveralyitem时,这不显示注意,如果我进行调试但没有看到错误... 为所有人而言

我找到了这段代码

 MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
    mapView.getOverlays().add(myLocationOverlay);
    myLocationOverlay.enableCompass(); // if you want to display a compass also
    myLocationOverlay.enableMyLocation();

显示我的位置,这么简单

我换了

OverlayItem [] listaPuntos = {new OverlayItem(哟,“Estas aqui”,“Marcado!”),};    dibujaPuntos();

这个

 MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
    mapView.getOverlays().add(myLocationOverlay);
    myLocationOverlay.enableCompass(); // if you want to display a compass also
    myLocationOverlay.enableMyLocation();

现在它有效..

how to put marker on map when location gets changed in android

0 个答案:

没有答案