By using Locator class one can get the location information of a given latitude and longitude. But for OS 7 if one uses locator class, it will give an exception. So in that case, one can use Google APIs to get the location address. The code given below is all about how to use the Google APIs to get complete address details of a given latitude and longitude.
===================================================================
public class Geocoder {
private static String networkString = “”;
private double[] coord = new double[2];
private String name = “”;
public double[] getAddressCordinate(String name) {
name = Split(name);
String url = “http://maps.googleapis.com/maps/api/geocode/xml?address=”
+ name + “&sensor=true”;
try {
if (getnetworkString()) {
HttpConnection connection = (HttpConnection) Connector.open(url
+ networkString);
connection.setRequestMethod(HttpConnection.GET);
InputStream istream = connection.openInputStream();
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
sp.parse(istream, new LocationParser(this, false));
istream.close();
connection.close();
}
} catch (Exception e) {
}
return coord;
}
private String Split(String name) {
String newName = “”;
for (int i = 0; i < name.length(); i++) {
if (name.charAt(i) == ‘ ‘)
newName += “%20″;
else
newName += name.charAt(i);
}
return newName;
}
private boolean getnetworkString() {
if (DeviceInfo.isSimulator()) {
networkString = “;deviceside=true;ConnectionTimeout=20000″;
return true;
}
else if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {
networkString = “;interface=wifi”;
return true;
}
else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) {
String carrierUid = getCarrierBIBSUid();
if (carrierUid == null) {
networkString = “;deviceside=true”;
return true;
} else {
networkString = “;deviceside=false;connectionUID=” + carrierUid
+ “;ConnectionType=mds-public”;
return true;
}
}
else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS) {
networkString = “;deviceside=false”;
return true;
}
else if (CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE) {
return false;
}
else {
networkString = “;deviceside=true”;
return true;
}
}
private String getCarrierBIBSUid() {
ServiceRecord[] records = ServiceBook.getSB().getRecords();
int currentRecord;
for (currentRecord = 0; currentRecord < records.length; currentRecord++) {
if (records[currentRecord].getCid().toLowerCase().equals(“ippp”)) {
if (records[currentRecord].getName().toLowerCase()
.indexOf(“bibs”) >= 0) {
return records[currentRecord].getUid();
}
}
}
return null;
}
public void upDateLocation(double latitude, double longitude) {
coord[0] = latitude;
coord[1] = longitude;
}
public String reverseGeocode(double lat, double lng) {
String url = “http://maps.google.com/maps/api/geocode/xml?sensor=false&latlng=”
+ lat + “,” + lng;
try {
if (getnetworkString()) {
HttpConnection connection = (HttpConnection) Connector.open(url
+ networkString);
connection.setRequestMethod(HttpConnection.GET);
InputStream istream = connection.openInputStream();
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
sp.parse(istream, new LocationParser(this, true));
istream.close();
connection.close();
}
} catch (Exception e) {
}
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class LocationParser extends DefaultHandler {
private boolean getName = false;
private boolean lat = false;
private boolean longi = false;
private double latitude = 0;
private double longitude = 0;
Geocoder geocoder;
private String address = “”;
private boolean addressfound = false;
public LocationParser(Geocoder geocoder, boolean getName) {
this.getName = getName;
this.geocoder = geocoder;
}
public void startDocument() throws SAXException {
super.startDocument();
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
if (localName.equals(“lat”))
lat = true;
if (localName.equals(“lng”))
longi = true;
if (localName.equals(“formatted_address”))
addressfound = true;
}
public void characters(char[] ch, int start, int length)
throws SAXException {
super.characters(ch, start, length);
if (lat) {
lat = false;
latitude = Double.parseDouble(new String(ch, start, length));
}
if (longi) {
longi = false;
longitude = Double.parseDouble(new String(ch, start, length));
}
if (addressfound) {
if (address.equals(“”))
address = new String(ch, start, length);
addressfound = false;
}
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
super.endElement(uri, localName, qName);
}
public void endDocument() throws SAXException {
super.endDocument();
if (getName == false)
geocoder.upDateLocation(latitude, longitude);
else
geocoder.setName(address);
}
}