Read This First
From Google Mapki
Here are a few things that keep turning up over and over again on the Google Maps API Discussion Group
[edit] Getting Started
There's useful information in the Official Documentation and in this Tutorial.
[edit] Post a link not a code snippet
If you need help getting your site working, always post a link to your map when you post queries to the Discussion Group. This allows the experts to use a Javascript debugger on your code, and to look at other parts of your page that might be related to the problem, such as your CSS, the format of any XML files, and the MIME information being returned from your webhost.
If you're working on a development version on your localhost machine, put a copy of the page on a real webpage before asking for help.
Please see these posts to the Google Maps API group: A matter of scope, how to output javascript from php, Map is gray in IE, but works in Firefox for more information.
[edit] Javascript Errors
Errors reported from Javascript often contain useful clues about why a page is not working. The errors are not automatically displayed.
In Internet Explorer, you must double-click the yellow triangular error icon in the status bar to view error messages.
In Firefox, open the Javascript Console from the Tools menu.
In Opera, open the Javascript Console from the Tools -> Advanced menu.
In Safari, first enable the Debug Menu by typing the following command in Terminal (while Safari is NOT running):
% defaults write com.apple.Safari IncludeDebugMenu 1
then, when Safari is running, select Debug -> Show Java Console.
[edit] Check your XML file format
GXmlHttp will not process data contained in an XML file that is not properly formatted. Even the slightest formatting error will cause it to fail to read any data.
To check the formatting, enter the URL of the XML file into your browser's address bar, or, on your local machine, drag the XML file into the browser.
IE has different rules for validating XML files, so check your XML formatting with IE as well as another browser.
[edit] My page works in Firefox but not in IE
IE will produce "Operation Aborted" and "Cannot load page" errors after partially rendering the map if the Javascript is contained inside a <table> or <div> or other incomplete element modifies that element.
The easiest fix is to move all your Javascript to the bottom of your HTML file, just before the </body>. Be sure that all open <tag> elements are closed with a matching </tag> before the script.
Also, MSIE doesn't allow characters in XML files that don't match the specified character set. Hint: <?xml version="1.0" encoding="UTF-8"?< is a good first guess for a character set.
And MSIE parses trailing commas in array declarations differently. Other browsers treat [A,B,C,] as an array with three entries, but MSIE treats it as if you had written [A,B,C,null]. This can cause a failure if you pass such an array to a function that isn't expecting the possibility of a null entry.
See other solutions in the FAQs#Browser Problems.
[edit] The wrong info window opens when I click a marker
Don't attempt to "unroll" the createMarker() function.
This doesn't work.
var point = new GLatLng(43.65654,-79.90138);
marker = new GMarker(point);
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml('Some stuff");
});
map.addOverlay(marker);
var point = new GLatLng(43.91892,-78.89231);
marker = new GMarker(point);
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml('Some other stuff");
});
map.addOverlay(marker);
What goes wrong is that there's only one copy of the "marker" variable. When the "click" event gets triggered at a later time, it gets the value that's in the variable at the time of the call, but you want the value at the time the event handler was set up. This happens through a special feature of the Javascript language called function closure.
What you should do is use a createMarker() function, like this, so that the function holds function closure on the marker and html variables.
function createMarker(point,html) {
var marker = new GMarker(point);
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml(html);
});
return marker;
}
var point = new GLatLng(43.65654,-79.90138);
createMarker(point, 'Some stuff');
map.addOverlay(marker);
var point = new GLatLng(43.91892,-78.89231);
createMarker(point, 'Some other stuff');
map.addOverlay(marker);
[edit] My API key doesn't work
- Do not try to use a map key for 'localhost' - whilst you will be allowed to create it, but in practice, it will prove unusable, presumably because the Google server which validates the key will interpret localhost as a reference to itself.
- Check that you are not using IE7beta1. It doesn't support API key checking.
- Make sure that you typed the URL correctly when you registered.
- Make sure that you didn't type in the URL of an individual web page. It's usually best to use the URL of the whole domain. You can register an individual directory, but you can't register an individual page.
- If you registered a directory, rather than the whole domain, then remember that directory names are CaSe SensiTivE.
- If you created your page in FrontPage, check that it hasn't changed your &s into &amp;. FP tries to be helpful, but actually breaks it. The way to get round it is to tell FP that you want & and it will change it to &. The guys that wrote FP never considered the possibility that you would enter the line correctly in the first place.
- If there's a possibility that you've got some clever redirection trickery that's changing the name of the page, type javascript:alert('http://'+window.location.host) into the address bar while your page is displayed. That's the domain name that the API sees, so that's the one that you need to register.
You can go back and re-register as many times as you like. If you spell the URL the same each time, you will receive the same key.
[edit] My map loads slowly when there are 1500 markers
There's a limit to how quickly the API can process active markers.
I recommend not having more than 200 active markers under API version 1, and no more than 150 for API version 2, which is slower.
You can have as many GMarker() objects as you like. It's the number of markers that have been placed on the map with map.addOverlay() which is significant.
If you need more markers, then you should look at strategies that reduce the number of markers that are displayed at any one time.
[edit] Zoom level jumps
"When I try to change the zoom level by one step, it jumps to a completely different value."
This is caused by using a string variable when you set the zoom level. If you're reading the zoom level value from a database, or from an input field on your page, you need to convert it to an integer with parseInt(zoom) before you use it. Similarly, x/y and latitude/longitude values should be converted to numbers using parseFloat(value).
What's happening is that Javascript allows unambiguous arithmetic to be performed on string variables that contain digits, so multiplication, division and subtraction all do the right thing. But if one of the operands is a string, the "+" operator is considered to be the "string concatenation" operator.
When the API code attempts to calculate the new zoom it does something like this
var newzoom = zoom + 1;
When "zoom" is a string of digits, like "6", then the "+" operator performs string concatenation, so "newzoom" is assigned the value "61" instead of "7".
[edit] Google Maps has different imagery
For countries other than the USA and Canada the same map tiles are displayed on API maps as on maps.google.com, but updated versions may be released to the API a few days later than to maps.google.com.
If your webpage is hosted on a domain owned by Google, such as googlepages, igoogle, or orkut, you can choose to use the Google Maps maps by omitting the API key when loading the API code. (If you're page isn't in a domain owned by Google, then you won't be able to display any maps if you omit the key). You could even consider renting a small amount of webspace on one of the Google domains, and using <iframe> to display that map within your non-Google page. (Note, however, that the API geocoder won't work without an API key).
[edit] How do I report incorrect map data
The Google Maps API feedback page allows you to report incorrect map data.
When the problem is geographically located, prepare a map in Google Maps showing the location. You can click the "Link to this page" to get a URL that allows anyone to display same map. In another browser window, verify that this link displays the problem. Copy this link from the address bar, and paste into the problem report message.
The copyright text at the bottom of the page indicates the data provider.
Errors on Tele Atlas streetmaps can be reported via Tele Atlas Insight. Check first if your problem has already been fixed in Insight, in which case the fix should eventually be rolled out to Google tiles.
Errors on NAVTEQ streetmaps can be reported via Navteq Map Reporter.
Errors on MapData Sciences streetmaps (used in Australia and New Zealand) can be reported via MapData Sciences
[edit] Geocoding UK, China or Japan
The Maps API geocoder provides detailed geocoding for Andorra, Australia, Austria, Belgium, Canada, France, Germany, Gibraltar, India, Italy, Japan (but only in Japanese), Liechtenstein, Luxembourg, Monaco, Netherlands, New Zealand, San Marino, Singapore, Spain, Sweden, Switzerland, Taiwan, United States of America, the United Kingdom and the Vatican City. (e.g. "4144 Avenue Pierre-De-Coubertin, Montréal, Canada").
For other it provides geocoding for country names and city names (e.g. "Nairobi, Kenya").
API Geocoding for Japan was announced on 7 Dec 2006 in the Official Blog. Street address queries are only supported in Japanese.
API Geocoding or the UK was announced on 6 July 2007 in the Official Blog The geocoding of cities in parts of the British Isles outside the UK, and in China was enabled at the same time (but street level geocoding or postcode geocoding is not supported there).
Geocoding of UK post codes is restricted for legal reasons. If you attempt to geocode just a UK post code, then only the first group of letters is used. E.g. if you attempt to geocode "FY2 1HH" then you'll get the location of "FY2".
[edit] Re-Geocoding
It's a Bad Idea, for several reasons, to use the Google Maps API Geocoder to re-geocode a list of addresses each time someone opens your page. It's very much better to geocode your addresses once, in advance, and store the coordinates.
- Geocoding takes quite a bit of computer power. Let's try to avoid needlessly wasting Google server resources.
- Geocoding your locations each time will make your page take longer to open. A geocode request can sometimes take as long as a second to be processed.
- There is a geocoding speed limit. If you run at a rate faster than the equivalent of 50000 requests per day (1.725 seconds per request) for several minutes, then Google will block you for a day. Consider that you might exceed the speed limit if several users happen to open your page at the same time.
- GClientGeocoder can't cope with two requests made within the same millisecond. It uses the millisecond timestamp to match the geocode replies to the requests. If someone with a fast computer opens your page, some of the geocode results will get lost. On an 8GHz computer something like 10% of geocode requests made from a tight loop will fail.
- There are awkward pitfalls for the unwary coder when there's more than one geocode request in flight at the same time.
If, for some obscure reason, you can't possibly geocode your addresses in advance, then do cache the results of any bulk geocoding that you perform on your server and make those results available to subsequent instances of your page.
[edit] Strange Line in Info Window
There's a strange line in the info window graphics in MSIE under API v2.79 to v2.90.
The documentation has always recommended that you use standards-compliant XHTML on pages that contain maps because the layout and behaviours much more predictable across browsers. Until API v2.79 it hasn't made any noticeable difference, so some people have been ignoring this advice.
In v2.79 to v2.90, the pieces of the info window graphics are slightly out of place in MSIE if you don't use the DOCTYPE recommended in the documentation.
It's fixed in v2.91
[edit] My Maps Placemark sorting
Can I sort the Placemarks in My Maps?
You can do this since 30 Jan 2008. "Now you can reorder items on your personally created maps by dragging and dropping them in the left panel. "
source: http://groups.google.com/group/google-maps-current-issues/browse_thread/thread/bc5b2833bedb0787#
[edit] My Maps Printing
The My Maps markers don't print.
Someone at Google forgot to upload the printIcon files for the My Maps markers.
You an either print from a screen capture or use Google Maps API in conjunction with the EgeoXml extension and specify {printgif:true} and provide a printgifpath that points to the set of printIcon files that you ant to use.
[edit] Polylines in MSIE
In order for polylines and polygons to be displayed in MSIE you must enable VML. See the official documentation.
In v2.83 and v2.84 this restriction was lifted. From v2.85 to v2.90 the restriction was imposed again. In v2.91 it is lifted again.
This applies to polylines and polygons that are created for you via GDirections and GGeoXml as well as those that you create yourself.
[edit] Geocoding sometimes fails
A new geocoding speed limits system was imposed on 2 Oct 2007.
Previously, you could make about 100 consecutive geocode requests without delays, and then you'd be blocked for 24 hours.
Under the new system, even a very small number of requests without delays can cause some to be rejected with error 620 G_GEO_TOO_MANY_QUERIES.
The easiest and best solution is to geocode your locations once, offline, store the coordinates in your database and use those instead of regeocoding the addresses every time you use them.
If that's not possible, then:
Add a delay between requests (I recommend 100ms but other people
have suggested 25ms or 250ms)
Use .getLocations instead of .getLatLng
Check the response code for error 620 and retry the request
after a delay.
Apart from the fact that that's more work to code, it's also going to run noticeably slower than using stored coordinates.
