简体   繁体   中英

How to show multiple markers in GOOGLE MAP

My doubt is how to show multiple markers on Google map.

I am getting latitude and longitude values of some vehicles through J SON web-service ,I am using separate java file for declarations called Util and I'm storing latitude in a array-list called latitudeall (Util.latitudeall) and longitude in the same way inside an array-list called (Util.longitudeall) and vehicle numbers inside the array-list called(Util.vehiclenumall).

in another java file I am trying to setup markers inside the fragment

Here is my code

FragmentManager fmanager = getSupportFragmentManager();

//googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

    SupportMapFragment mapFragment =  (SupportMapFragment) fmanager.findFragmentById(R.id.map);
    googleMap = mapFragment.getMap();

    for (int i = 0; i < Util.vehiclenumall.size(); i++) {     
     googleMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)).position( new LatLng(Double.parseDouble(Util.latitudeall.get(i)), Double.parseDouble(Util.longitudeall.get(i)))));
       }}

Above code is not working and how can i set the markers by getting the latitude and longitude values from array-list......

Here is the response of the Util.latitudeall(Latitude numbers) array-list in my log-cat

enter code here
latitude number----[8.150896666666666666666666667, 
                    8.182241666666666666666666667,  
                    13.262706666666666666666666667,
                    8.50547, 8.849243333333333333333333333,  
                    8.157979999999999666666666667]

Here is the response of the Util.longitudeall(Longitude numbers) array list in my log cat

longitude number----[
                 77.478666666666666666666666667, 
                 77.412475, 80.03815333333333333333333333,
                  76.970696666666666666666666667,
                  76.637001666666666666666666667, 
                   77.462568333333333333333333333]

And this is the response of Util.vehiclenumall(Vehicle numbers) array-list in my log-cat

vehicle number----[
               TN-74-AD-8178, 
                TN-74-AE-1900, 
                TN-20-CC-2543, 
                KL-01-7890, 
                TN-74-AD-7463,
                 TN-20-P-3220]

My task is to call a class inside oncreate().when that class is called the markers has to set automatically in the google map.

MY Question is : HOW TO CALL THESE ARRAY LIST VALUES AND SET MARKERS INSIDE GOOGLE MAP(INSIDE THE CODE OF FRAGMENT)

PLEASE HELP ME...

Thanks in Advance....

Please note I am not an Android developer and my Maps experience lies in Web applications but why here:

   for(int i=0;i<Util.vehiclenumall.size();i++)
{
     googleMap.addMarker(new MarkerOptions().title(Util.vehiclenumall.get(i)).
            position(new LatLng(Util.latitudeall.get(i).length(),Util.longitudeall.get(i).length())).title("hello")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));}googleMap.setMyLocationEnabled(true);googleMap.setMapType(mapType);}

Are you calling .length() after the Util.latitudeall.get(i). I'm presuming that the get method returns the value at that index of the array. Length will thus provide an invalid long and lat. I suggest changing this to:

for(int i=0;i<Util.vehiclenumall.size();i++)
{
     googleMap.addMarker(new MarkerOptions().title(Util.vehiclenumall.get(i)).
            position(new LatLng(Util.latitudeall.get(i),Util.longitudeall.get(i))).title("hello")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));}googleMap.setMyLocationEnabled(true);googleMap.setMapType(mapType);}

The length call will give the the length of the returned value and not the value itself so for example if your latitude is 8.150896666666666666666666667 then Latitude.length() will be 29. This will be passed to the marker as a location but is not actually valid geographically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM