Using Google static maps
Using Google static maps
The article discusses the main possibilities of using Google static maps based on the author’s own experience.
Google provides two options for using its map services.
- Google maps, a JavaScript-based API that provides a variety of options for displaying various geographic information.
There is a well-designed and structured class library, the documentation for which can be found at the following links:
English , Russian and play in the sandbox
- And the second option provides much less functionality, but sometimes it is only possible to use it.
These are static maps. In general terms, using this service you can simply display a map, and the maximum size of the resulting image is 512x512 pixels, also, together with the coordinates, it is permissible to indicate the zoom level and add additional marks.
Both service options have their advantages and should be used in well-defined areas. But in order to correctly determine when which cards to use, you need to know all the possibilities.
This article is written about static cards, you can talk about ordinary cards another time.
Static google maps
To work with the Google API, you need to register for free and get an API key, which is then used in most cases.
Also, for developers, complete information on the use of the Static Maps API is provided, which is located at the following addresses: English and Russian .
There are times when you really want to dynamically use maps in a project, but you can’t use regular Google maps. This is mainly due to the presence of JavaScript support in the target platform.
A good example, J2ME, is pretty decent networking opportunities, but there is absolutely no way to use JavaScript. Therefore, static maps is one of the best solutions.
Consider a hypothetical mobile service: Users can freely add their own bars, cafes and other catering facilities to the database, after which, based on the address entered, the coordinates of the place are calculated and then on the page with its description you can immediately see the map.
It can be seen that the task is divided into two subtasks:
- Get coordinates at;
- Get the map by coordinates.
You can also add another third subtask, organize navigation on the map.
Getting coordinates at
This procedure is called geocoding, there is also reverse-geocoding, when an address is given based on the coordinates, but we will not consider it.
A detailed description is located at the following addresses: English and Russian
For successful geocoding, you only need two things: an address in a format that Google understands and the API key.
If everything is simple with the first, that the term of understandability for Google requires clarification. The fact is that for a guaranteed successful geocoding, the entered address must be as complete and accurate as possible, because, for example, in the same America you can easily find Moscow and Paris. There is also a requirement of the fullness of the address, for example, if the street is called “Red Shooters”, then the request “Cr. Shooters ”most likely nothing will be found.
Then everything is simple, the coordinates are provided as a pair of values, latitude and longitude, and you need to send the address of interest to the Google service
http://maps.google.com/maps/geo indicating the following parameters:
- q - Address subject to geocoding;
- key - your API key;
- output - The required response format, the following values are possible here: xml, kml, csv or json.
An example of obtaining a random address in Moscow:
http://maps.google.com/maps/geo?q=Russia%20Moscow%20Tverskaja%2011&output=json&key=abcdefg
UPD 1 I will transfer my comment to the article, I think this is the place for it.
___________________
There are still some optional parameters, for example, the format parameter, to receive the image not in the default GIF, but in JPG, or PNG (my favorite).
I’ll
give you all the others: maptype - The type of requested map can be satellite, terrain, hybrid besides mobile.
path- You can specify the drawing of a line between two points, and you can specify a lot of points. But this option is only suitable if you build these lines manually, since you can’t build a serious map with your hands, and automatically it doesn’t take into account streets, rivers, etc. It simply draws a line.
Although it is possible to get a path from one point to another, taking all of this into account, but only when using the regular JS API.
The coordinates of the nodal points are again separated by the pipe symbol "|".
Example: path = rgba: 0xff0000ff, weight: 5 | 40.737102, -73.990318 | 40.749825, -73.987963 | 40.752946, -73.987384 | 40.755823, -73.986397
span- The parameter is taken into account only if zoom is not specified, since using the span you can specify the size of the displayed area. For example, center = 40.714728, -73.998672 & span = 20.20 displays the center of Manhattan and the area around the size of 20x20 degrees.
frame - If present, a blue frame with a width of 5 pixels will be displayed around the map.
sensor - In the current version is marked as mandatory. As I understand it, it should be set equal to true if its own coordinates come from a moving device.
Well, the last one, hl - Allows you to explicitly specify the language for displaying all the names on the map. Understands a limited number of locale names.
___________________
I think it will not be difficult to parse the received data, especially since this is done differently in different programming languages, but I will not dwell on this.
As a result, we got either zeros if something went wrong, or the coordinates of the address that you want to save to the database, since it is very expensive to deal with geocoding every time.
Getting map by coordinates
This second part of the task arises as soon as we need to display a map. In principle, everything here is also simple, you need to use the second Google service:
http://maps.google.com/staticmap
It accepts the following parameters:
- center - Coordinates of the map center (format 37.6095373,55.7606587);
- zoom - zoom level of the map, in general, a number from 1 to 15;
- size - The dimensions of the requested image are transmitted in the form of WxH, for example 300x200, and the maximum values are defined as 512x512 pixels;
key - your API key; - maptype - Map type, several options are possible here, but in this case the value mobile is most suitable;
- markers - Using markers, you can add additional tags to the map, for example, show the surrounding cafes. The usage format is as follows:
{latitude}, {longitude}, {color} {letter} markers are separated by the pipe symbol "|"
Example:
markers = 40.702147, -74.015794, blues | 40.711614, -74.012318, greeng
Two markers are created here, the first blue with the letter S, the second green with the letter G.
As a result of the request generated by these rules, we get a normal image that is easy display on the screen.
There is still the question of organizing navigation, but this is already the details of the application, in the simplest case, next to the map, you can place links for with neighboring coordinates and to increase / decrease (zoom + 1, zoom-1) and load a new map each time, or organize something more complicated with asynchronous requests and map redrawing, in principle on J2ME it is not very difficult.
06/06/12
Pavel Osipov