Search This Blog

Monday, 19 March 2012

How to add multiple markers on MapField.


As far as i faced this issue and messed up my whole app while trying to draw the markers on map,which should be clickable i somehow solved it....So here i am giving you this tip-off.

The theory says, that whenever a screen changes the paint method is recalled ,like if we move the map or we can say pan technically...the thing which happens is that the paint method of MapField is recalled....so here is the code you need to follow to draw multiple markers on MapField in Blackberry

public void paint(Graphics g){
                  
                super.paint(g);
               
                 for(int i=0;i<=_deviceId.size()-1;i++){
                 
                Coordinates coords = getCoordinates();
                coords.setLatitude(Double.parseDouble((String) _latitude.elementAt(i)));
                coords.setLongitude(Double.parseDouble((String) _longitude.elementAt(i)));
               
               
                convertWorldToField(coords,_xypoint);
             
               x1[i] = _xypoint.x;
               y1[i] = _xypoint.y;
               if(_accStatus.elementAt(i).equals("0")){
                   g.drawText((String)_name.elementAt(i),_xypoint.x,_xypoint.y-10);
                   g.drawBitmap(_xypoint.x,_xypoint.y,_loc.getWidth(),_loc.getHeight(),_loc, 0, 0);
               }else{
                   g.drawText((String)_name.elementAt(i),_xypoint.x,_xypoint.y-10);
                   g.drawBitmap(_xypoint.x,_xypoint.y,_locOn.getWidth(),_locOn.getHeight(),_locOn, 0, 0);
               }
               } }
               };
         What i am doing here is i started a for loop , which will run for the no. of devices i have in "_deviceId" Vector.Then i am setting the latitude and longitude of Coordinates object"coords",here you have seen that when i am initializing it i set it to "getCoordinates()" this will get the coordinates of MapField we are using.after this i am converting the latitude and longitude which i got from server and i saved in Vectors "_latitude" and "_longitude" to the display coordinates i.e (xy Coordinates),after that it is simple , i am drawing an icon on that xyCoordinates which i get after conversion,so this will draw all the markers as all it is done in for loop.Don't confuse urself that what will happen if the xyCoordinates you will get is out of drawable area .....put it in try and catch ,everytime you pan the MapField this paint function will b called and whole of the process run,so there is no issue that on panning the map your markers will stay where they are they will also move to their original place......comment for any queries...

No comments:

Post a Comment