Posted on 9 Comments

Convert GPS Coordinates to Address using Google Geocoding API using Java

Google Logo

Google Maps makes it easy to convert addresses to their corresponding GPS coordinates using the Geocoding API. But what if you want to do the reverse, i.e. convert GPS coordinates to the corresponding address? Simple, just use Google’s ReverseGeocoding functionality.

Below you find a simple Java example of how to use the API to convert GPS coordinates to addresses:

package at.kerstner.geocoding;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

 * @param lng
 * @param lat
 * @return
    private String getAddressByGpsCoordinates(String lng, String lat)
            throws MalformedURLException, IOException, org.json.simple.parser.ParseException {
        URL url = new URL(""
                + lat + "," + lng + "&sensor=true");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        String formattedAddress = "";

        try {
            InputStream in = url.openStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String result, line = reader.readLine();
            result = line;
            while ((line = reader.readLine()) != null) {
                result += line;

            JSONParser parser = new JSONParser();
            JSONObject rsp = (JSONObject) parser.parse(result);

            if (rsp.containsKey("results")) {
                JSONArray matches = (JSONArray) rsp.get("results");
                JSONObject data = (JSONObject) matches.get(0); //TODO: check if idx=0 exists
                formattedAddress = (String) data.get("formatted_address");

            return "";
        } finally {
            return formattedAddress;

The example above retrieves the formatted address of the first result found. Additional checks have been omitted for better readability.

The API response has the following format:

"formatted_address":"Entenplatz 1, 8020 Graz, Austria",



You also might want to checkout for an easy way to handle and convert coordinates and preview them on a map.

Posted on Leave a comment

Google Maps update fails on HTC smartphone

Android Robot Logo

Recently I wanted to upgrade Google Maps on my HTC Desire running Android 2.3.3. Unfortunately the update kept failing with the following error

failed to download update…

After several attempts to restart the download process I gave up and restarted the phone and tried again – without success. Then I realized that the automatic update was enabled for this app. So, I disabled it, restarted the phone and the update process – with success!

I have reproduced this problem on a HTC Wildfire and a HTC Legend using different Android versions. It seems like the latest version of Google Maps requires the automatic update to be disabled to run the update? At least it worked this way and I have enabled the automatic update again – let’s see if it works out of the box for the next update. UPDATE: Today another update became available, so I tried my luck. Unfortunately, I had to disable the automatic update, restart the phone and run the update manually again.

Posted on 2 Comments

Draggable Pin Marker for Google Maps API V3

Google Logo

Google’s Maps API offer a vast range of functionality to interact with underlying maps. As opposed to statically setting markers (pins) programmatically sometimes you might want your users to be able to interact with the map by dragging existing markers around the map.

Google Maps API V3

Fortunately, Google’s JavaScript API offers this functionality out of the box. Below you’ll find an example based on Google Maps API V3. Basically, what we want to achieve is to be automatically notified when a user changes a marker’s position. Clearly, this can be done by registering a listener on the marker which will fire an event once the marker has been dropped on the destination coordinates. After all, we want to be given these coordinates in order to carry out further actions. But first of all we need to create a new map instance and bind it to a DOM container element, as explained in the official reference documentation:

var latLng = new google.maps.LatLng(lat,lng);

var options = {
  zoom: 13,
  center: latLng,
  scrollwheel: false,
  disableDoubleClickZoom: true,
  mapTypeId: google.maps.MapTypeId.ROADMAP,
  locale: LANG

var map = new google.maps.Map(document.getElementById(canvasDomId), options);

Once we have the map instance we are able to add markers, as shown in the following code snippet:

var marker = new google.maps.Marker({
  position: latLng, 
  map: map, 
  title: title,
  draggable: true

In order for the marker to be draggable across the map we need to set the attribute draggable to true. Finally, in order to be notified of changes of the marker’s position we need to add an event listener to the marker:

google.maps.event.addListener(marker, 'drag', function(event) {
  console.debug('new position is '' / '+event.latLng.lng()); 

google.maps.event.addListener(marker, 'dragend', function(event) {
  console.debug('final position is '' / '+event.latLng.lng()); 

Whereas the event drag is fired whenever the user drags the marker and slides it across the map, the dragend is triggered only when the marker is dropped.

Working example

<script src=""></script>

var iconBase = 'http://base-url-to-your-icon';

function initialize() {
var map_canvas = document.getElementById('map_canvas');
var map_options = {
center: new google.maps.LatLng(TODO_LAT, TODO_LNG),
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
var map = new google.maps.Map(map_canvas, map_options);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(TODO_LAT, TODO_LNG),
map: map,
icon: iconBase + 'TODO_img.png'

var features = [{
position: new google.maps.LatLng(TODO_LAT, TODO_LNG),
type: 'info'


google.maps.event.addListener(marker, 'drag', function(event) {
console.debug('new position is '' / '+event.latLng.lng());

google.maps.event.addListener(marker, 'dragend', function(event) {
console.debug('final position is '' / '+event.latLng.lng());

google.maps.event.addDomListener(window, 'load', initialize);


<div id="map_canvas" style="width: 500px;height: 400px;"></div>