Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Geocoding in Google Earth API using Maps V3

I have developed an application a while ago using GE plugin. In that application, I use the Geocode function which depends of course on (maps, 2.xx). With the deprecation of Google Maps V2, this part of the code is no longer working. My code is based on the sample application for geocoding in the Google Earth Demo site (http://earth-api-samples.googlecode.com/svn/trunk/examples/geocoder.html) which no longer works either.

I searched the v3 site but couldn't find a way of dealing with this. Using google.load("maps", "3.xx");
instead of google.load("maps", "2.xx"); simply doesn't work and I get a rejection from the Google Maps server.

like image 941
user3024098 Avatar asked Aug 10 '26 22:08

user3024098


1 Answers

The reason using google.load("maps", "3.xx"); doesn't work is that you MUST supply the sensor parameter. i.e. google.load('maps','3.6', { other_params: 'sensor=false' });

The sensor parameter of the URL must be included, and indicates whether this application uses a sensor (such as a GPS locator) to determine the user's location. We've left this example as a variable set_to_true_or_false to emphasize that you must set this value to either true or false explicitly.

See: https://developers.google.com/maps/documentation/javascript/tutorial#Loading_the_Maps_API

Anyhow, I made a working example of using the Google Maps V3 Geocoder with the Earth Api for you to see how it works.

Also here is a code example that geocodes the term "New York" and moves to the first result found once the plugin and maps api have loaded (just in case jsfiddle disappears in future...)

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('earth', '1');
google.load('maps','3.6', { other_params: 'sensor=false' }); // or true

var ge = null; // GEPlugin
var geocoder = null; // GClientGeocoder

var init = function() { 
  google.earth.createInstance('map3d', initCallback, failureCallback);
};

var initCallback = function(object) {
  ge = object;
  geocoder = new window.google.maps.Geocoder(); //v3 Geocoder
  ge.getWindow().setVisibility(true);

  // for example: geocode New York
  geocode("New York");
};

var failureCallback = function(error) {
  alert("Plugin Error: " + error);
};

var geocode = function(address) {
  geocoder.geocode({ 'address': address }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        // do something with the result, such as flying to it...
        var point = results[0].geometry.location;
        var lookat = ge.createLookAt('');
        lookat.set(point.lat(), point.lng(), 100, ge.ALTITUDE_RELATIVE_TO_GROUND, 0, 0, 1000);
        ge.getView().setAbstractView(lookat);
    } else {
        alert("Geocode Error: " + status);
    }
  });
};

google.setOnLoadCallback(init);
</script>
like image 139
Fraser Avatar answered Aug 14 '26 14:08

Fraser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!