How to add markers to my map dynamically?

Asked

Viewed 131 times

1

Good guys, I’m developing a mobile application in AS, and I’m using the map to add markers of each location of a new registered form, IE, when performing a registration I send along my latitude and longitude to the server. On the map I take this list of objects passing latitude and longitude and try to add markers to these positions, but I can’t do it dynamically. Can someone help me?

The code that generates the map with the object’s locations:

private void gerarMapa(){

    for(int i = 0; i < palhetas.size(); i++) {
        double lat = Double.parseDouble( palhetas.get(i).getEndereco().getLatitude() );
        double lon = Double.parseDouble( palhetas.get(i).getEndereco().getLongitude() );
        LatLng locale = new LatLng( lat, lon );

        MarkerOptions options = new MarkerOptions()
                .position( locale )
                .title( palhetas.get(i).getCodigo() )
                .snippet( palhetas.get(i).getEndereco().getRua() )
                .icon( BitmapDescriptorFactory
                        .fromResource( R.drawable.marker ) );
        mMap.addMarker( options );
        mMap.moveCamera( CameraUpdateFactory.newLatLng( locale ) );
    }
}

The way it is, it only marks the location of the first object. Another problem I have is to get the location of the object, I first need to convert to double, because my object I’m bringing via gson.

Gson gson = new Gson();
Type usuariosListType = new TypeToken<ArrayList<Palheta>>(){}.getType();
palhetas = gson.fromJson(response, usuariosListType);
  • The code to add the markers seems to be correct. The palette list is with the . size() returning more than one?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.