Ajuda com longitude e latitude

4 respostas
D

fiz o seguinte codigo para pegar a longitude e latitude com o endereço.. so q esta caindo no catch com este erro "Invalid byte 2 of 2-byte UTF-8 sequence.'

meu codigo ficou asim
alguem poderia me ajudar?

package com.controlmobile.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.controlmobile.entity.LongitudeLatitude;

public class SearchLongLat {

	public LongitudeLatitude getLongLat(String address) {
		try {
			String thisLine;
			address = address.replace(' ', '+');
			URL u = new URL(
					"http://maps.google.com/maps/geo?q=\'"
							+ address
							+ "\'&output=xml&ABQIAAAA-p9H5A-4ByAkCnGpnWvmVRSZBikiXOrbbl0eyFwjsYfXLs5q5hRqCRL35w0zeDa-5m96RVKojxlhhA");
			LongitudeLatitude longitudeLatitude = new LongitudeLatitude();
			BufferedReader theHTML = new BufferedReader(new InputStreamReader(
					u.openStream()));

			FileWriter fstream = new FileWriter("url.xml");
			BufferedWriter out = new BufferedWriter(fstream);
			while ((thisLine = theHTML.readLine()) != null)
				out.write(thisLine);
			out.close();

			File file = new File("url.xml");
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(file);
			doc.getDocumentElement().normalize();
			NodeList nl = doc.getElementsByTagName("code");
			Element n = (Element) nl.item(0);
			String st = n.getFirstChild().getNodeValue();

			if (st.equals("200")) {
				NodeList n2 = doc.getElementsByTagName("coordinates");
				Element nn = (Element) n2.item(0);
				String longLat = nn.getFirstChild().getNodeValue();
				String[] list = longLat.split(";");
				for (String longLatUN : list) {
					String[] content = longLatUN.split(",");

					longitudeLatitude.setLatitude(Double.parseDouble(content[0]));
					longitudeLatitude.setLongitude(Double.parseDouble(content[1]));

				}

				return longitudeLatitude;

			} else {
				return null;
			}
		} catch (MalformedURLException e) {
			System.out.println("error 1 ="+e.getMessage());
			return null;
		} catch (IOException e) {
			System.out.println("error 2 ="+e.getMessage());
			return null;
		} catch (Exception ex) {
			System.out.println("error 3 =" + ex.getMessage());
			return null;
		}

	}

}

4 Respostas

otaviojava

Cara acredito que se tiver espaço pode estar prejudicando o seu código.

Dá uma olhada nesse projeto é bem semelhante ao que você estar querendo.

http://code.google.com/p/geocodingbr/

D

espaço aonde?

otaviojava

No seu parâmetro address.
Se tiver espaço modifica para “+”

D

la no addres ja tem um replace subistituindo para +
estou perdido isso, nao consigo achar nda q possa me ajuda… esse codigo funcionou alguns dias, e hj fui testar e nao funcionou

Criado 25 de novembro de 2011
Ultima resposta 25 de nov. de 2011
Respostas 4
Participantes 2