Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 5.29 KB  |  hits: 27  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Show marker on google map position obtained from xml
  2. public class XMLParsingExample1 extends MapActivity {
  3.     /**
  4.      * Create Object For SiteList Class
  5.      */
  6.     SitesList sitesList = null;
  7.     private MapController mapController;
  8.     private MapView mapView;
  9.     private LocationManager locationManager;
  10.     private String ss1;
  11.     private String ss2;
  12.     GeoPoint p;
  13.  
  14.     class MapOverlay extends com.google.android.maps.Overlay {
  15.         @Override
  16.         public boolean draw(Canvas canvas, MapView mapView,
  17.                             boolean shadow, long when) {
  18.             super.draw(canvas, mapView, shadow);
  19.  
  20.             //---translate the GeoPoint to screen pixels---
  21.             Point screenPts = new Point();
  22.             mapView.getProjection().toPixels(p, screenPts);
  23.  
  24.             //---add the marker---
  25.             Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.marker);
  26.             canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
  27.             return true;
  28.         }
  29.     }
  30.  
  31.     /**
  32.      * Called when the activity is first created.
  33.      */
  34.     @Override
  35.     public void onCreate(Bundle savedInstanceState) {
  36.         super.onCreate(savedInstanceState);
  37.         setContentView(R.layout.main);
  38.  
  39.         /** Create a new layout to display the view */
  40.  
  41.  
  42.         try {
  43.  
  44.             /** Handling XML */
  45.             SAXParserFactory spf = SAXParserFactory.newInstance();
  46.             SAXParser sp = spf.newSAXParser();
  47.             XMLReader xr = sp.getXMLReader();
  48.  
  49.             /** Send URL to parse XML Tags */
  50.             URL sourceUrl = new URL(
  51.                     "http://site4demo.com/artealdiaonline/output.php?lat=-34.6394879&lng=-58.3617837kkj");
  52.  
  53.             /** Create handler to handle XML Tags ( extends DefaultHandler ) */
  54.             MyXMLHandler myXMLHandler = new MyXMLHandler();
  55.             xr.setContentHandler(myXMLHandler);
  56.             xr.parse(new InputSource(sourceUrl.openStream()));
  57.  
  58.         } catch (Exception e) {
  59.             System.out.println("XML Pasing Excpetion = " + e);
  60.         }
  61.  
  62.         /** Get result from MyXMLHandler SitlesList Object */
  63.         sitesList = MyXMLHandler.sitesList;
  64.  
  65.         ArrayList<Integer> Latitude = new ArrayList<Integer>();
  66.         ArrayList<Integer> Longtitude = new ArrayList<Integer>();
  67.         for (int i = 0; i < sitesList.getName().size(); i++) {
  68.  
  69.         // **Is this correct??**
  70.             ss1 = sitesList.getName().get(i);
  71.  
  72.         }
  73.         for (int i = 0; i < sitesList.getWebsite().size(); i++) {
  74.             ss2 = sitesList.getWebsite().get(i);
  75.         }
  76.  
  77.  
  78.         RelativeLayout linearLayout = (RelativeLayout) findViewById(R.id.mainlayout);
  79.         mapView = (MapView) findViewById(R.id.mapview);
  80.         mapView.setBuiltInZoomControls(true);
  81.         //mapView.setStreetView(true);
  82.         mapView.setSatellite(true);
  83.         mapController = mapView.getController();
  84.         mapController.setZoom(14); // Zoon 1 is world view
  85.         locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  86.         locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
  87.                 0, new GeoUpdateHandler());
  88.  
  89.         //Here i pass latitude and longitude value for displaying in map
  90.  
  91.         String coordinates[] = {ss1, ss2};
  92.         double lat = Double.parseDouble(coordinates[0]);
  93.         double lng = Double.parseDouble(coordinates[1]);
  94.         p = new GeoPoint(
  95.                 (int) (lat * 1E6),
  96.                 (int) (lng * 1E6));
  97.  
  98.         mapController.animateTo(p);
  99.         mapController.setZoom(17);
  100.  
  101.         //---Add a location marker---
  102.         MapOverlay mapOverlay = new MapOverlay();
  103.         List<Overlay> listOfOverlays = mapView.getOverlays();
  104.         listOfOverlays.clear();
  105.         listOfOverlays.add(mapOverlay);
  106.         mapView.invalidate();
  107.     }
  108.  
  109.     @Override
  110.     protected boolean isRouteDisplayed() {
  111.         return false;
  112.     }
  113.  
  114.     public class GeoUpdateHandler implements LocationListener {
  115.         @Override
  116.         public void onLocationChanged(Location location) {
  117.             int lat = (int) (location.getLatitude() * 1E6);
  118.             int lng = (int) (location.getLongitude() * 1E6);
  119.             GeoPoint point = new GeoPoint(lat, lng);
  120.             mapController.animateTo(point); //  mapController.setCenter(point);
  121.         }
  122.  
  123.         @Override
  124.         public void onProviderDisabled(String provider) {
  125.         }
  126.  
  127.         @Override
  128.         public void onProviderEnabled(String provider) {
  129.         }
  130.  
  131.         @Override
  132.         public void onStatusChanged(String provider, int status, Bundle extras) {
  133.         }
  134.     }
  135. }
  136.        
  137. /** Variables */
  138. private ArrayList<String> Latitude = new ArrayList<String>();
  139. private ArrayList<String> Longitude = new ArrayList<String>();
  140. private ArrayList<String> category = new ArrayList<String>();
  141.  
  142.  
  143. /** In Setter method default it will return arraylist
  144.  *  change that to add  */
  145.  
  146. public ArrayList<String> getName() {
  147.     return Latitude;
  148. }
  149.  
  150. public void setName(String name) {
  151.     this.Latitude.add(name);
  152. }
  153.  
  154. public ArrayList<String> getWebsite() {
  155.     return Longitude;
  156. }
  157.  
  158. public void setWebsite(String website) {
  159.     this.Longitude.add(website);
  160. }
  161.  
  162. public ArrayList<String> getCategory() {
  163.     return category;
  164. }
  165.  
  166. public void setCategory(String category) {
  167.     this.category.add(category);
  168. }
  169.        
  170. double lat = Double.parseDouble(coordinates[0]);
  171. double lng = Double.parseDouble(coordinates[1]);