public void loadLocationsAsync(String kml) { List<LotLocation> locations = new ArrayList<LotLocation>(); try { XMLReader parser = XMLReaderFactory.createXMLReader("org.ccil.cowan.tagsoup.Parser"); InputStream is = new ByteArrayInputStream(kml.getBytes()); // build out an XML document using TagSoup Document doc = new Builder(parser).build(is); // set the ns of the document as the XPathContext or we will not find the elements when we // attempt to parse XPathContext context = new XPathContext("ns", "http://www.w3.org/1999/xhtml"); // get the Placemark nodes within the data Nodes nodes = doc.query(".//ns:Placemark", context); for (int index = 0; index < nodes.size(); index++) { LotLocation placemark = new LotLocation(); Node placemarkNode = nodes.get(index); Node nameNode = placemarkNode.query("ns:name", context).get(0); if (nameNode != null) placemark.setLocation(nameNode.getValue()); Node descriptionNode = placemarkNode.query("ns:description", context).get(0); if (descriptionNode != null) placemark.setDescription(descriptionNode.getValue()); Node lnglatNode = placemarkNode.query("ns:Point/ns:coordinates", context).get(0); if (lnglatNode != null) { // get longitude,latitude,altitude, per KML spec String[] points = lnglatNode.getValue().split(","); placemark.setPoint( new LatLng( Double.parseDouble(points[1].trim()), Double.parseDouble(points[0].trim()))); } locations.add(placemark); } // spin off a new thread and load locations new LoadLocationsTask().execute(locations); } catch (Exception e) { Log.e("LoadLocationsTask", "Failure attempting to load locations", e); } }
@Override protected TreeMap<Float, LotLocation> doInBackground(android.location.Location... location) { TreeMap<Float, LotLocation> locations = new TreeMap<Float, LotLocation>(); try { // null checking party ahead, majority stems form no connection, perhaps a connection // manager could centralize this if (location[0] != null) { android.location.Location currentLocation = location[0]; AmazonSimpleDBClient instance = SimpleDB.getInstance(); if (instance != null) { SelectResult result = instance.select( new SelectRequest("select * from `Locations` where active = '1' limit 50")); if (result != null) { for (Item lot : result.getItems()) { android.location.Location lotLocation = new android.location.Location(currentLocation.getProvider()); LotLocation internalLotLocation = new LotLocation(); internalLotLocation.setId(lot.getName()); // used to temporarily store hours of open and close for each location Hashtable<Integer, String> hoursOpen = new Hashtable<Integer, String>(); Hashtable<Integer, String> hoursClose = new Hashtable<Integer, String>(); for (Attribute attribute : lot.getAttributes()) { if (attribute.getName().equalsIgnoreCase("location")) internalLotLocation.setLocation(attribute.getValue()); if (attribute.getName().equalsIgnoreCase("rating")) internalLotLocation.setRating(Float.parseFloat(attribute.getValue())); if (attribute.getName().equalsIgnoreCase("business")) internalLotLocation.setBusiness(attribute.getValue()); if (attribute.getName().equalsIgnoreCase("description")) internalLotLocation.setDescription(attribute.getValue()); if (attribute.getName().equalsIgnoreCase("amex")) internalLotLocation.setAcceptsAmex( com.experiment.trax.utils.Boolean.valueOf(attribute.getValue())); if (attribute.getName().equalsIgnoreCase("visa")) internalLotLocation.setAcceptsVisa( com.experiment.trax.utils.Boolean.valueOf(attribute.getValue())); if (attribute.getName().equalsIgnoreCase("mastercard")) internalLotLocation.setAcceptsMastercard( com.experiment.trax.utils.Boolean.valueOf(attribute.getValue())); if (attribute.getName().equalsIgnoreCase("discover")) internalLotLocation.setAcceptsDiscover( com.experiment.trax.utils.Boolean.valueOf(attribute.getValue())); if (attribute.getName().equalsIgnoreCase("phone")) internalLotLocation.setPhone(attribute.getValue()); if (attribute.getName().equalsIgnoreCase("verified")) internalLotLocation.setVerified( com.experiment.trax.utils.Boolean.valueOf(attribute.getValue())); // create a key -> value to day of week and time opened or closed if (attribute.getName().equalsIgnoreCase("1_open")) hoursOpen.put(1, attribute.getValue()); if (attribute.getName().equalsIgnoreCase("1_close")) hoursClose.put(1, attribute.getValue()); if (attribute.getName().equalsIgnoreCase("2_open")) hoursOpen.put(2, attribute.getValue()); if (attribute.getName().equalsIgnoreCase("2_close")) hoursClose.put(2, attribute.getValue()); if (attribute.getName().equalsIgnoreCase("3_open")) hoursOpen.put(3, attribute.getValue()); if (attribute.getName().equalsIgnoreCase("3_close")) hoursClose.put(3, attribute.getValue()); if (attribute.getName().equalsIgnoreCase("4_open")) hoursOpen.put(4, attribute.getValue()); if (attribute.getName().equalsIgnoreCase("4_close")) hoursClose.put(4, attribute.getValue()); if (attribute.getName().equalsIgnoreCase("5_open")) hoursOpen.put(5, attribute.getValue()); if (attribute.getName().equalsIgnoreCase("5_close")) hoursClose.put(5, attribute.getValue()); if (attribute.getName().equalsIgnoreCase("6_open")) hoursOpen.put(6, attribute.getValue()); if (attribute.getName().equalsIgnoreCase("6_close")) hoursClose.put(6, attribute.getValue()); if (attribute.getName().equalsIgnoreCase("7_open")) hoursOpen.put(7, attribute.getValue()); if (attribute.getName().equalsIgnoreCase("7_close")) hoursClose.put(7, attribute.getValue()); if (attribute.getName().equalsIgnoreCase("lat")) lotLocation.setLatitude(Double.parseDouble(attribute.getValue())); if (attribute.getName().equalsIgnoreCase("lng")) lotLocation.setLongitude(Double.parseDouble(attribute.getValue())); } // store the times the lot is opened and closed internalLotLocation.setHoursOpen(hoursOpen); internalLotLocation.setHoursClose(hoursClose); // store the location of the lot internalLotLocation.setPoint( new LatLng(lotLocation.getLatitude(), lotLocation.getLongitude())); // save it to our locations listing to return back to the consumer locations.put(currentLocation.distanceTo(lotLocation), internalLotLocation); } } } } } catch (Exception e) { Log.e("GetLocationsTask", "Failure attempting to get locations", e); } return locations; }