Difference between revisions of "Teams Winter 2011/team1/BlackBerry/Add Mapping Option"

From CDOT Wiki
Jump to: navigation, search
Line 60: Line 60:
 
       {
 
       {
 
         Student student = (Student) _keywordFilterField.getSelectedElement();
 
         Student student = (Student) _keywordFilterField.getSelectedElement();
             
+
               
        if(student != null)
+
                if(student != null)
        {                  
+
                {                  
            try
+
                    try
            {                      
+
                    {                      
              String address = student.getAddress();
+
                        String address = student.getAddress();
              //start coordinates
+
                        //start coordinates
              Coordinates co = new Coordinates(45.423488, -75.697929, 0);
+
                        showAddress(address);                          
              Landmark[] landm = Locator.geocode(address, co);
+
                       
              //Get latitude and longitude
+
                    } catch(LocatorException ex){
              double lon = landm[0].getQualifiedCoordinates().getLongitude()*100000.0 ;
+
                        Dialog.alert("cannot get location: " + ex.getMessage());
              double lat = landm[0].getQualifiedCoordinates().getLatitude()*100000.0;
+
                    }
                //create XML document
+
                    catch(Exception ex){
              StringBuffer document = new StringBuffer("<location-document><location lon='");
+
                        Dialog.alert("map application is not available: "+ ex.getMessage());
              document.append(lon).append("' lat='").append(lat).append("' label='").append(address);
+
                    }
              document.append("' description='Student Address' zoom='10'/></location-document>");
+
                }
                     
 
              // Invoke maps application for current Contact
 
              Invoke.invokeApplication(Invoke.APP_TYPE_MAPS, new MapsArguments(MapsArguments.ARG_LOCATION_DOCUMENT, document.toString()));
 
                     
 
              } catch(LocatorException ex){
 
                  Dialog.alert("cannot get location: " + ex.getMessage());
 
              } catch(Exception ex){
 
                  Dialog.alert("map application is not available: "+ ex.getMessage());
 
            }
 
 
             }
 
             }
           }
+
        }));
     }));
+
</source>
 +
10.5. Implement <code>showAddress()</code> method:
 +
<source lang="java">
 +
  public void showAddress(String adrs)throws LocatorException{     
 +
    final String address = adrs;
 +
    new Thread(new Runnable(){         
 +
        public void run(){
 +
        try{         
 +
          Coordinates co = new Coordinates(45.423488, -75.697929, 0);     
 +
         
 +
          Landmark[] landm = Locator.geocode(address, co); 
 +
       
 +
          //Get latitude and longitude
 +
          double lon = landm[0].getQualifiedCoordinates().getLongitude()*10000.0 ;
 +
          double lat = landm[0].getQualifiedCoordinates().getLatitude()*10000.0;
 +
          //create mapview
 +
          MapView mapView = new MapView();
 +
          mapView.setLatitude((int)lat);
 +
          mapView.setLongitude((int)lon);
 +
          mapView.setZoom(10);
 +
         
 +
          // Invoke maps application for current Contact
 +
           Invoke.invokeApplication(Invoke.APP_TYPE_MAPS, new MapsArguments(mapView));
 +
       
 +
        } catch(LocatorException ex){
 +
            Dialog.alert("Cannot find location: " + ex.getMessage());
 +
        } catch(Exception ex){
 +
            Dialog.alert("Exception when getting location: " + ex.getMessage());
 +
        }
 +
      }
 +
     }).start();              
 +
     
 +
  }
 
  </source>
 
  </source>

Revision as of 14:45, 8 April 2011

10. Add Mapping Option

10.1. In order to implement Address Locator we need to add address field and getter/setter methods to Student class:

  public Student(String firstName, String lastName, String email, String address) {
      this.firstName = firstName;
      this.lastName = lastName;
      this.email = email;
      this.email = address; //add address field
   }

   public String getAddress(){
      return address;
   }
   public void setAddress(String address){
      this.address = address;
   }

10.2. Add EditTextFields to Add, Edit and view option:
viewItem

   BasicEditField addressField = new BasicEditField("address: ",student.getAddress(),50,Field.NON_FOCUSABLE);
   addressField.setBorder(roundedBorder);
   addressField.setBackground(solidBackground);
   add(addressField);

addItem

   EditField inputField4 = new EditField("Student's address: ","");           
   addDialog.add(inputField4);

   // Display the dialog and add a new element to the list
   if(addDialog.doModal() == 0) {
     addElementToList(new Student(inputField1.getText(),inputField2.getText(),inputField3.getText(), inputField4.getText()));                                                    
   }

editItem

 
  EditField inputField4 = new EditField("Address: ",studentOld.getAddress());           
  editDialog.add(inputField4);

  if(editDialog.doModal() == 0)
    {
      studentNew.setFirstName(inputField1.getText());
      studentNew.setLastName(inputField2.getText());
      studentNew.setEmail(inputField3.getText());
      studentNew.setAddress(inputField4.getText()); //set Address
      editListElement(studentOld, studentNew);                                           
    }

10.3. Add the Map Option to the manu:

   //Show the address on the map
   ImageMenuItem mapItem = new ImageMenuItem("Address on Map", 500, 5, MENU_MAP);
   _mainScreen.addMenuItem(mapItem);

10.4. Implement address locator:

   mapItem.setCommand(new Command(new CommandHandler(){
      public void execute(ReadOnlyCommandMetadata metadata, Object context)
      {
         Student student = (Student) _keywordFilterField.getSelectedElement();
                
                if(student != null)
                {                    
                    try
                    {                        
                        String address = student.getAddress();
                        //start coordinates
                        showAddress(address);                            
                        
                    } catch(LocatorException ex){
                        Dialog.alert("cannot get location: " + ex.getMessage());
                    }
                    catch(Exception ex){
                        Dialog.alert("map application is not available: "+ ex.getMessage());
                    }
                }
            }
        }));

10.5. Implement showAddress() method:

  public void showAddress(String adrs)throws LocatorException{       
     final String address = adrs;
     new Thread(new Runnable(){           
        public void run(){
        try{           
          Coordinates co = new Coordinates(45.423488, -75.697929, 0);       
           
          Landmark[] landm = Locator.geocode(address, co);   
         
          //Get latitude and longitude
          double lon = landm[0].getQualifiedCoordinates().getLongitude()*10000.0 ;
          double lat = landm[0].getQualifiedCoordinates().getLatitude()*10000.0;
          //create mapview
          MapView mapView = new MapView();
          mapView.setLatitude((int)lat);
          mapView.setLongitude((int)lon);
          mapView.setZoom(10);
           
          // Invoke maps application for current Contact
          Invoke.invokeApplication(Invoke.APP_TYPE_MAPS, new MapsArguments(mapView));
         
        } catch(LocatorException ex){
            Dialog.alert("Cannot find location: " + ex.getMessage());
        } catch(Exception ex){
            Dialog.alert("Exception when getting location: " + ex.getMessage());
        }
       }
     }).start();               
       
   }