How to implement a gps location on mapsforge ?

1,455 views
Skip to first unread message

Damien Silva

unread,
May 26, 2013, 7:48:45 PM5/26/13
to mapsfo...@googlegroups.com
Hello everyone,
I would like to implement a button which will active the gps and locate the person... But i do not know how to implement it ....
I tried that code, and it isnt working properly ...
Can someone help me please ?
I'm using the version 0.3.0   .


public class Mapa extends MapActivity{


    protected static Context MainActivity = null;

    public CapaTap itemizedOverlay;

    public Drawable defaultMarker;

    Drawable gpsMarker;

    ArrayItemizedOverlay gpsItemizedOverlay;

    

    ArrayList<SQLloja> listaLoja;

    Double altitude;

    Double longitude;

    Integer zoom;

    LocationManager lm;

    MapView mapView ;

    

@Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        

        try {

listaLoja = new loadDatabase().execute().get();

} catch (InterruptedException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

} catch (ExecutionException e) {

// TODO Auto-generated catch block

e.printStackTrace();

        

        //Action Bar is the bar on the top of each activity !

        ActionBar actionBar = getActionBar();

actionBar.setDisplayHomeAsUpEnabled(true);

Intent intent = getIntent();

zoom = (Integer) intent.getSerializableExtra("zoom");

altitude = (Double) intent.getSerializableExtra("altitude");

longitude = (Double) intent.getSerializableExtra("longitude");

MainActivity=this;

gpsMarker = getResources().getDrawable(R.drawable.mapsredpoiicon);

defaultMarker = getResources().getDrawable(R.drawable.markergold);

        itemizedOverlay = new CapaTap(defaultMarker, MainActivity);

        gpsItemizedOverlay = new ArrayItemizedOverlay(gpsMarker);

        

        for(SQLloja loja : listaLoja){

        String informacoesLoja = "Rua : "+loja.getMorada()+"\nC.P. : "+loja.getCodigo_postal()+"\nTel. : "+loja.getTelefone();

        itemizedOverlay.addItem(new OverlayItem(new GeoPoint(loja.getLatitude(), loja.getLongitude()),"Loja Cidadao "+loja.getNome(),informacoesLoja));

        }

        

        mapView = new MapView(this, new MapnikTileDownloader());

        mapView.setClickable(true);

        mapView.setBuiltInZoomControls(true);

        //mapView.setTextScale(4f); 

        setContentView(mapView); 

        

        // Ajustar el zoom y el centro del mapa

        mapView.setCenter(new GeoPoint(altitude, longitude));

        mapView.zoom((byte) 2, 0);  

        

        mapView.getOverlays().add(itemizedOverlay);

        Log.d("GPS3",Integer.toString(mapView.getOverlays().size()));

        mapView.getController().setZoom(zoom);

        

        // MapScaleBar

        MapScaleBar scaleBar = mapView.getMapScaleBar();

        scaleBar.setShowMapScaleBar(true);


        

        /*---------------*/

        

       

        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    


        LocationListener locationListener = new MyLocationListener();


        lm.requestLocationUpdates( LocationManager.GPS_PROVIDER,  0, 0, locationListener);

        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10, 20, locationListener);


        

    }


    public class CapaTap extends ArrayItemizedOverlay {

private final Context context;

CapaTap(Drawable defaultMarker, Context context) {

super(defaultMarker);

this.context = context;

}


@Override

protected boolean onTap(final int index) {

OverlayItem item = createItem(index);

if(item!=null) {

Builder builder=new AlertDialog.Builder(context);

builder.setIcon(R.drawable.lojacidadaosmall);

builder.setTitle(item.getTitle());

builder.setMessage(item.getSnippet());

builder.setNegativeButton("OK", null);

builder.setPositiveButton("Go To", new DialogInterface.OnClickListener() {

          public void onClick(DialogInterface dialog, int id) {

            Intent openMenu = new Intent("com.example.basicmaponline.INFOLOJA");

      openMenu.putExtra("loja",listaLoja.get(index));

      startActivity(openMenu);

           

          }

      });

builder.show();

}

return super.onTap(index);

}

}

    

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        //getMenuInflater().inflate(R.menu.activity_main, menu);

    MenuInflater inflater = getMenuInflater();

    inflater.inflate(R.menu.homeactionbar, menu);

    

    return true;

    }

    

  //Botao para retroceder ( Action Bar)

  @Override

  public boolean onOptionsItemSelected(MenuItem item) {

  // TODO Auto-generated method stub

  if(item.getItemId()==R.id.lojasHome){

  Intent intent = new Intent("com.example.basicmaponline.MENU");

              intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

              startActivity(intent);

  }

  else {

  super.onBackPressed();

  }

  

  return true;

  }

    

    

    

    public class loadDatabase extends AsyncTask<Void, Void, ArrayList<SQLloja>>{


@Override

protected ArrayList<SQLloja> doInBackground(Void... params) {

ArrayList<SQLloja> listaLojas = new ArrayList<SQLloja>();

try {

PostgreSQL pSQL = new PostgreSQL();

listaLojas = pSQL.getLojasCidadaoByOrder();

} catch (SQLException e) {

e.printStackTrace();

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

return listaLojas;

}

@Override

protected void onPostExecute(ArrayList<SQLloja> listaLojas){

listaLoja = listaLojas;

}

}

    

    /* Class My Location Listener */


    public class MyLocationListener implements LocationListener {


    @Override


    public void onLocationChanged(Location loc){

    double latitude = loc.getLatitude();

    double longitude = loc.getLongitude();

    Log.d("GPS","Latitude: "+latitude+" Longitude: "+longitude);

    Log.d("GPS2",Integer.toString(mapView.getOverlays().size()));

    

    GeoPoint point = new GeoPoint(latitude, longitude);

    //gps.addItem(new OverlayItem(point, null, null));

    //mapView.getOverlays().add(gps);

    //mapView.getOverlays().get(2).requestRedraw();

    //Marker marker = new Marker(R.drawable.mapsredpoiicon,point);

    //mapView.getOverlays().get(3).r

    

    

    OverlayItem item = new OverlayItem(point, null,null);

    gpsItemizedOverlay.addItem(item);

    mapView.getOverlays().add(itemizedOverlay);

    }


    @Override

    public void onProviderDisabled(String provider){

    }



    @Override

    public void onProviderEnabled(String provider){

    }



    @Override

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

    }


    } /*End of Class MyLocationListener*/

    

    

}

Damien Silva

unread,
May 26, 2013, 7:52:06 PM5/26/13
to mapsfo...@googlegroups.com
I forgo to include my .java 
Mapa.java

Ludwig

unread,
May 26, 2013, 9:02:03 PM5/26/13
to mapsfo...@googlegroups.com
Hi Damien,

I have not read your code (it is too much), but there is a MyLocationOverlay class in the source code that should help you achieve what you want. http://code.google.com/p/mapsforge/source/browse/mapsforge-map/src/main/java/org/mapsforge/android/maps/overlay/MyLocationOverlay.java

An app itself cannot turn the GPS on or off, this is a user setting but you can check if GPS is enabled and ask a user to enable it if is not.

Hope this helps

Ludwig



--
You received this message because you are subscribed to the Google Groups "mapsforge-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mapsforge-de...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Damien Silva

unread,
May 27, 2013, 4:13:34 AM5/27/13
to mapsfo...@googlegroups.com
Well, i saw that code already recently. The problem is : the API of that code isn't available for my API mapsforge 0.3.0
For example: i dont have the "getOverlayController().redrawOverlays();" method... as well the Marker class too ! Do i need to install the googlemap API for that use ?

Ludwig

unread,
May 27, 2013, 4:38:51 AM5/27/13
to mapsfo...@googlegroups.com
I would strongly suggest you use the master version, there are many bugs in the 0.3.0 version that have received fixes since the release of 0.3.0. (E.g. the 0.3.0 version does not run on Android 4.2)

Ludwig

Damien Silva

unread,
May 27, 2013, 5:58:30 AM5/27/13
to mapsfo...@googlegroups.com
where can i get that version ?
And my android phone is a samsung galaxy s4 which comes with version 4.2 and works perfectly , without the GPS thing ... ( It shows the map and put some POI on my map too) 

Ludwig

unread,
May 28, 2013, 6:00:31 AM5/28/13
to mapsfo...@googlegroups.com
Sorry, that one commit I was thinking off is maybe in 0.3.0, but several others are missing (deadlock, memory leaks).

Ludwig
Message has been deleted
Message has been deleted
Message has been deleted

Damien Silva

unread,
May 28, 2013, 9:29:10 AM5/28/13
to mapsfo...@googlegroups.com
Oh i  appreciate the response and the .jar ! :) 
Just i have one problem, how am i able to solve these questions with the newest build of mapsforge ?

ex.: "

mapView = new MapView(thisnew MapnikTileDownloader());" It doesnt recognize now
Or OverlayItem doesnt recognize too.

Reply all
Reply to author
Forward
0 new messages