1.1 --- a/java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/ext/Geo.java Mon May 24 20:20:55 2010 +0200
1.2 +++ b/java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/ext/Geo.java Sun Jun 20 14:46:47 2010 +0200
1.3 @@ -16,104 +16,104 @@
1.4 */
1.5 public class Geo {
1.6
1.7 - private static final Logger log = Logger.getLogger(Geo.class.getSimpleName());
1.8 + private static final Logger log = Logger.getLogger(Geo.class.getSimpleName());
1.9
1.10 - /**
1.11 - * Převede poštovní adresu na zeměpisné souřadnice.
1.12 - * @param adresa
1.13 - * @return souřadnice k dané adrese nebo null, v případě chyby.
1.14 - */
1.15 - public Souradnice getSouradnice(String adresa) {
1.16 - try {
1.17 + /**
1.18 + * Převede poštovní adresu na zeměpisné souřadnice.
1.19 + * @param adresa
1.20 + * @return souřadnice k dané adrese nebo null, v případě chyby.
1.21 + */
1.22 + public Souradnice getSouradnice(String adresa) {
1.23 + try {
1.24
1.25 - /**
1.26 - * TODO: naprosto zprasené → předělat →
1.27 - * http://code.google.com/intl/cs/apis/maps/documentation/geocoding/#XMLParsing
1.28 - */
1.29 - DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
1.30 - Document d = db.parse(sestavURL(adresa));
1.31 - NodeList mista = d.getElementsByTagName("location");
1.32 - Node misto = mista.item(0);
1.33 - NodeList potomci = misto.getChildNodes();
1.34 - String delka = null;
1.35 - String sirka = null;
1.36 - for (int i = 0; i < potomci.getLength(); i++) {
1.37 - Node p = potomci.item(i);
1.38 - if ("lat".equals(p.getNodeName())) {
1.39 - sirka = p.getTextContent();
1.40 + /**
1.41 + * TODO: naprosto zprasené → předělat →
1.42 + * http://code.google.com/intl/cs/apis/maps/documentation/geocoding/#XMLParsing
1.43 + */
1.44 + DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
1.45 + Document d = db.parse(sestavURL(adresa));
1.46 + NodeList mista = d.getElementsByTagName("location");
1.47 + Node misto = mista.item(0);
1.48 + NodeList potomci = misto.getChildNodes();
1.49 + String delka = null;
1.50 + String sirka = null;
1.51 + for (int i = 0; i < potomci.getLength(); i++) {
1.52 + Node p = potomci.item(i);
1.53 + if ("lat".equals(p.getNodeName())) {
1.54 + sirka = p.getTextContent();
1.55 + }
1.56 + if ("lng".equals(p.getNodeName())) {
1.57 + delka = p.getTextContent();
1.58 + }
1.59 + }
1.60 +
1.61 + if (sirka == null || delka == null) {
1.62 + log.log(Level.WARNING, "Selhalo zjišťování souřadnic – šířka nebo délka jsou null – pro adresu: " + adresa);
1.63 + return null;
1.64 + } else {
1.65 + return new Souradnice(Double.parseDouble(sirka), Double.parseDouble(delka));
1.66 + }
1.67 + } catch (Exception e) {
1.68 + log.log(Level.WARNING, "Selhalo zjišťování souřadnic pro adresu: " + adresa, e);
1.69 + return null;
1.70 }
1.71 - if ("lng".equals(p.getNodeName())) {
1.72 - delka = p.getTextContent();
1.73 - }
1.74 - }
1.75 -
1.76 - if (sirka == null || delka == null) {
1.77 - log.log(Level.WARNING, "Selhalo zjišťování souřadnic – šířka nebo délka jsou null – pro adresu: " + adresa);
1.78 - return null;
1.79 - } else {
1.80 - return new Souradnice(Double.parseDouble(sirka), Double.parseDouble(delka));
1.81 - }
1.82 - } catch (Exception e) {
1.83 - log.log(Level.WARNING, "Selhalo zjišťování souřadnic pro adresu: " + adresa, e);
1.84 - return null;
1.85 - }
1.86 - }
1.87 -
1.88 - private static String sestavURL(String adresa) throws UnsupportedEncodingException {
1.89 - return "http://maps.google.com/maps/api/geocode/xml?sensor=false&address=" + URLEncoder.encode(adresa, "UTF-8");
1.90 - }
1.91 -
1.92 - public class Souradnice {
1.93 -
1.94 - private double sirka;
1.95 - private double delka;
1.96 -
1.97 - @Override
1.98 - public String toString() {
1.99 - return "šířka = " + sirka + "; délka = " + delka + ";";
1.100 }
1.101
1.102 - @Override
1.103 - public boolean equals(Object o) {
1.104 - if (o instanceof Souradnice) {
1.105 - Souradnice s = (Souradnice)o;
1.106 - return s.sirka == sirka && s.delka == delka;
1.107 - } else {
1.108 - return false;
1.109 - }
1.110 + private static String sestavURL(String adresa) throws UnsupportedEncodingException {
1.111 + return "http://maps.google.com/maps/api/geocode/xml?sensor=false&address=" + URLEncoder.encode(adresa, "UTF-8");
1.112 }
1.113
1.114 - @Override
1.115 - public int hashCode() {
1.116 - int hash = 5;
1.117 - hash = 79 * hash + (int) (Double.doubleToLongBits(this.sirka) ^ (Double.doubleToLongBits(this.sirka) >>> 32));
1.118 - hash = 79 * hash + (int) (Double.doubleToLongBits(this.delka) ^ (Double.doubleToLongBits(this.delka) >>> 32));
1.119 - return hash;
1.120 + public class Souradnice {
1.121 +
1.122 + private double sirka;
1.123 + private double delka;
1.124 +
1.125 + @Override
1.126 + public String toString() {
1.127 + return "šířka = " + sirka + "; délka = " + delka + ";";
1.128 + }
1.129 +
1.130 + @Override
1.131 + public boolean equals(Object o) {
1.132 + if (o instanceof Souradnice) {
1.133 + Souradnice s = (Souradnice) o;
1.134 + return s.sirka == sirka && s.delka == delka;
1.135 + } else {
1.136 + return false;
1.137 + }
1.138 + }
1.139 +
1.140 + @Override
1.141 + public int hashCode() {
1.142 + int hash = 5;
1.143 + hash = 79 * hash + (int) (Double.doubleToLongBits(this.sirka) ^ (Double.doubleToLongBits(this.sirka) >>> 32));
1.144 + hash = 79 * hash + (int) (Double.doubleToLongBits(this.delka) ^ (Double.doubleToLongBits(this.delka) >>> 32));
1.145 + return hash;
1.146 + }
1.147 +
1.148 + public Souradnice(double sirka, double delka) {
1.149 + this.sirka = sirka;
1.150 + this.delka = delka;
1.151 + }
1.152 +
1.153 + public String getLoc() {
1.154 + return "Loc: " + sirka + ", " + delka;
1.155 + }
1.156 +
1.157 + public double getDelka() {
1.158 + return delka;
1.159 + }
1.160 +
1.161 + public double getSirka() {
1.162 + return sirka;
1.163 + }
1.164 +
1.165 + public void setDelka(double delka) {
1.166 + this.delka = delka;
1.167 + }
1.168 +
1.169 + public void setSirka(double sirka) {
1.170 + this.sirka = sirka;
1.171 + }
1.172 }
1.173 -
1.174 - public Souradnice(double sirka, double delka) {
1.175 - this.sirka = sirka;
1.176 - this.delka = delka;
1.177 - }
1.178 -
1.179 - public String getLoc() {
1.180 - return "Loc: " + sirka + ", " + delka;
1.181 - }
1.182 -
1.183 - public double getDelka() {
1.184 - return delka;
1.185 - }
1.186 -
1.187 - public double getSirka() {
1.188 - return sirka;
1.189 - }
1.190 -
1.191 - public void setDelka(double delka) {
1.192 - this.delka = delka;
1.193 - }
1.194 -
1.195 - public void setSirka(double sirka) {
1.196 - this.sirka = sirka;
1.197 - }
1.198 - }
1.199 }