Como todos sabem, o Google disponibiliza localização espacial por meio de mapas na web.
Dessa forma podemos trabalhar com o google maps no mobile usando a J2ME Google Maps API. Primeiramente é necessário registrar sua API Key neste link.
Para tal tarefa vamos construir um exemplo bem simples que a Nokia disponibilizou.
Primeiro criamos um MIDlet para chamar a classe GoogleMaps.java, já implementada pelo Forum Nokia.
Veja:
public void startMIDlet() {
//O código ABQIAAAA8... é chave do javamovel para o acesso ao GoogleMaps
GoogleMaps gMap = new GoogleMaps("ABQIAAAA8RV1IH5-33d1jdRb-T54FRR-7z_Fstom-aTuluzN7CpsnUxh0hTTbmJYA-f-AJATiEBUsF_lLzDBcw");
try {
double[] lanLng = gMap.geocodeAddress("Dourados - MS");
} catch (Exception ex) {
ex.printStackTrace();
}
try {
//Os args: tamanho da tela, latitude, longitude, zoom, figura
Image map = gMap.retrieveStaticImage(320, 240, -22.22373,-54.78443, 14, "png32");
getForm().append(map);
} catch (IOException ex) {
ex.printStackTrace();
}
switchDisplayable(null, getForm());
}
Agora usamos a classe GoogleMaps.java:
Nosso exemplo GoogleMaps API para o mobile está pronto! Agora laçamos o desafio para melhorarem o código, colocando zoom no mapa e buscando por nome de cidades por exemplo. Use as referências para melhorar o código.
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Image;
public class GoogleMaps {
private static final String URL_UNRESERVED =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"abcdefghijklmnopqrstuvwxyz" +
"0123456789-_.~";
private static final char[] HEX = "0123456789ABCDEF".toCharArray();
// these 2 properties will be used with map scrolling methods. You can remove them if not needed
public static final int offset = 268435456;
public static final double radius = offset / Math.PI;
private String apiKey = null;
public GoogleMaps(String key) {
apiKey = key;
}
public double[] geocodeAddress(String address) throws Exception {
byte[] res = loadHttpFile(getGeocodeUrl(address));
String[] data = split(new String(res, 0, res.length), ',');
if (data[0].compareTo("200") != 0) {
int errorCode = Integer.parseInt(data[0]);
throw new Exception("Google Maps Exception: " + getGeocodeError(errorCode));
}
return new double[] {
Double.parseDouble(data[2]), Double.parseDouble(data[3])
};
}
public Image retrieveStaticImage(int width, int height, double lat, double lng, int zoom,
String format) throws IOException {
byte[] imageData = loadHttpFile(getMapUrl(width, height, lng, lat, zoom, format));
return Image.createImage(imageData, 0, imageData.length);
}
private static String getGeocodeError(int errorCode) {
switch (errorCode) {
case 400:
return "Bad request";
case 500:
return "Server error";
case 601:
return "Missing query";
case 602:
return "Unknown address";
case 603:
return "Unavailable address";
case 604:
return "Unknown directions";
case 610:
return "Bad API key";
case 620:
return "Too many queries";
default:
return "Generic error";
}
}
private String getGeocodeUrl(String address) {
return "http://maps.google.com/maps/geo?q=" + urlEncode(address) + "&output=csv&key="
+ apiKey;
}
private String getMapUrl(int width, int height, double lng, double lat, int zoom, String format) {
return "http://maps.google.com/staticmap?center=" + lat + "," + lng + "&format="
+ format + "&zoom=" + zoom + "&size=" + width + "x" + height + "&key=" + apiKey;
}
private static String urlEncode(String str) {
StringBuffer buf = new StringBuffer();
byte[] bytes = null;
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeUTF(str);
bytes = bos.toByteArray();
} catch (IOException e) {
// ignore
}
for (int i = 2; i < b =" bytes[i];">= 0) {
buf.append((char) b);
} else {
buf.append('%').append(HEX[(b >> 4) & 0x0f]).append(HEX[b & 0x0f]);
}
}
return buf.toString();
}
private static byte[] loadHttpFile(String url) throws IOException {
byte[] byteBuffer;
HttpConnection hc = (HttpConnection) Connector.open(url);
try {
hc.setRequestMethod(HttpConnection.GET);
InputStream is = hc.openInputStream();
try {
int len = (int) hc.getLength();
if (len > 0) {
byteBuffer = new byte[len];
int done = 0;
while (done < bos =" new" buffer =" new" count =" is.read(buffer))">= 0 ) {
bos.write(buffer, 0, count);
}
byteBuffer = bos.toByteArray();
}
} finally {
is.close();
}
} finally {
hc.close();
}
return byteBuffer;
}
private static String[] split(String s, int chr) {
Vector res = new Vector();
int curr;
int prev = 0;
while ( (curr = s.indexOf(chr, prev)) >= 0 ) {
res.addElement(s.substring(prev, curr));
prev = curr + 1;
}
res.addElement(s.substring(prev));
String[] splitted = new String[res.size()];
res.copyInto(splitted);
return splitted;
}
}
Download do projeto no netbeans
Resultado:
Referências:
http://wiki.forum.nokia.com/index.php/J2ME_Google_Maps_API
http://code.google.com/intl/pt-BR/apis/maps/signup.html