Thursday, July 11, 2013

Java - Generate Image from Google Maps

Beside this API, Google Maps also provides an URL based API for extracting static Google Maps directly into desktop application, without requiring JavaScript or access keys. This API is very well explained here: http://code.google.com/apis/maps/documentation/staticmaps/index.html.

public class ImageUtils {
/**
* zoom level of the map, which determines the magnification level of the map.
* 0-21
*/
private static int zoom = 16;
/***
* scale:
* the number of pixels that are returned. scale=2 returns twice as many pixels as scale=1
*/
private static int scale = 2;
/***
* image size
* - free: 640x640
* - business: 2048x2048
*/
private static String size = "640x640";

/***
* maptype:
* roadmap, satellite, hybrid, and terrain.
*/
private static String maptype = "roadmap";

/***
* specifies whether the application requesting the static map is using a sensor to determine the user's location
*/
private static boolean sensor = false;

/***
* defines a marker of location.
* ie, color:blue, label:A
*/
private static String markers = "color:blue%7Clabel:A%7C";

public static void main(String[] args) throws IOException{
String imageFile = "d:\\plot.png";
String address = "Donaufelder straße 16, 1040 Wien, Österreich";
generateGoogleImage(address,imageFile);
}

private static File generateGoogleImage(String address,String imageFile){
address = address.replace(" ","+");
File googleImage = new File(imageFile);
try {
String addressGoogleUrl = "http://maps.googleapis.com/maps/api/staticmap?" +
    "center=" + address +
    "&zoom=" + zoom +
    "&size=" + size +
    "&scale=" + scale +
    "&maptype="+ maptype +
    "&markers=" + markers +
    address+
    "&sensor="+ sensor;

System.out.println("Address!"+"\t"+addressGoogleUrl);
   BufferedImage img = ImageIO.read(new URL(addressGoogleUrl));
   ImageIO.write(img, "png", googleImage);
   System.out.println("\n\t"+googleImage.getPath());
   } catch (Exception ex) {
        System.out.println("Error!" + ex);
   }
return googleImage;
}
}

Reference:
http://e-blog-java.blogspot.co.at/2011/12/display-static-google-maps-in-java.html

Related Posts:

  • JavaScript - Determines whether a value is an illegal number The isNaN() function determines whether a value is an illegal number (Not-a-Number). This function returns true if the value is NaN, and false if not. Syntax isNaN(value) Example Example Check whether a number is a… Read More
  • Java - Memory Analyzer A. To generate HEAP DUMP in Memory Analyzer: Project>Run Configuration> Arguments -XX:+HeapDumpOnOutOfMemoryError Heap dump will be generated when the out of memory occurs B. Use MAT to open Heap Dump Analyzin… Read More
  • Eclipse - Uninstall Plugin How do I remove a plug-in? You should not remove plug-ins from Eclipse. Plug-ins should be installed as features using the Update Manager. The same Update Manager can be used to disable plug-ins by disabling th… Read More
  • JavaScript - setTimeOut vs setIntervalWith JavaScript, it is possible to execute some code at specified time-intervals. This is called timing events. It's very easy to time events in JavaScript. The two key methods that are used are: setInterval() - executes a … Read More
  • jQuery - serializeArray serializeArray for uncheck checkboxes Trick: <input type='hidden' name='check' value='false'/> <input type='checkbox' name='check' value='true'/> jQuery.serializeArray(); serializeArray fieldset doesn't … Read More

0 comments:

Post a Comment