Search code examples
javaandroidgoogle-maps-markerslatitude-longitudegoogle-latitude

Display Map Markers from Lat Lon


I am learning android. Right now I am working on an app that uses map api. So far I did the following: - I long clicked the map and opened a new activityClickedMap displaying lat and lon of the location I clicked - I clicked save button and added lat and lon, both to server and to my local db

The thing I want to do is this: - When I open the map I want it to display a marker using the lat and the lon from database


Solution

  • Retrieve lat and long from database.

    String  latfromdb , longfromdb ;
    

    and then

    LatLng latLng = new LatLng(Double.parseDouble(latfromdb),Double.parseDouble(longfromdb));

     MarkerOptions optionss = new MarkerOptions()
                        .alpha(1)
                        .flat(false)
                        .position(latLng)
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.pin));
                googleMap.addMarker(optionss);
                googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
                googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
    

    Get the list of lat long from db and Add Marker in for loop for multiple marker :

    MarkerOptions optionss = new MarkerOptions()
                        .alpha(1)
                        .flat(false)
                        .position(latLng)
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.pin));
                googleMap.addMarker(optionss);