0

I'm trying to get mi location (by gps or wifi its same), and never obtains, if i debug there are a bucle in gotLocation() method. I trying with lot of methods by always the same result: Here my class`s

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





}

And this is the location class

//Script 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);
}

}

Because of this, when i try to draw mi olveralyitem this don't show noting, and if i make debug but don't see the error... Thaks for all

I find this code

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

to show my location, its so simple

i replaced

OverlayItem [] listaPuntos = {new OverlayItem(yo, "Estas aqui", "Marcado!"),}; dibujaPuntos();

for this

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

and now it works..

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

Community
  • 1
  • 1
colymore
  • 11,776
  • 13
  • 48
  • 90
  • If you've found a solution please post it as an answer and mark it as accepted. That way stackoverflow has less unanswered questions :) – Martin Foot Dec 07 '11 at 20:37
  • 1
    Users with less than 100 reputation can't answer their own question for 8 hours after asking. You may self-answer in 7 hours. Until then please use comments, or edit your question instead. – colymore Dec 07 '11 at 20:49

0 Answers0