I'm doing this to add Google Earth to Google Maps v3:
google.load("earth", "1");
// map options
var options = mapster.MAP_OPTIONS,
element = document.getElementById('map-canvas'),
// map
map = mapster.create(element, options);
var ge = new GoogleEarth(map);
The script on my html file looks like this:
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script src="https://www.google.com/jsapi"></script>
<script src="googleearth.js"></script>
<script src="script.js"></script>
I keep getting Uncaught google.earth not loaded and not sure what I did wrong.
I'm using this reference: http://google-maps-utility-library-v3.googlecode.com/svn/trunk/googleearth/docs/reference.html
Here's my full code in case it helps: http://plnkr.co/edit/NlPF3F259IIMgj2pfN09?p=preview
I really want to add Google Earth to Google Maps like the one in here: http://maps.google.com
Is it possible with v3?
Yes - it is possible to integrate the Earth Api with the Maps v3 API - here is a working example.
The problem with your code is the line var ge = new GoogleEarth(map); - you are making this call before the Earth api has finished loading via the asynchronous call to google.load("earth", "1");
This means that google.earth is null when you try and use it and so the Uncaught google.earth not loaded error is thrown.
To fix it you should only call new GoogleEarth(map) once the API has finished loading. The easiest way to do this is probably to use a callback.
For example - you could amend your script.js as follows.
(function(window, google, mapster) {
google.load("earth", "1");
// map options
var options = mapster.MAP_OPTIONS,
element = document.getElementById('map-canvas'),
map = mapster.create(element, options);
// callback method to fire once the api had loaded
google.maps.event.addDomListener(window, 'load', function() { new GoogleEarth(map) });
var marker2 = map.addMarker({
lat: 37.781350,
lng: -122.485883,
draggable: true,
events: [{
name: 'click',
callback: function(e, marker) {
console.log(e, marker);
}
}, {
name: 'dragend',
callback: function() {
alert('dragged');
}
}]
});
// no point calling this here as google.earth will be null
//var ge = new GoogleEarth(map);
}(window, google, window.Mapster));
Related
function dest()
{
var options = {
types: ['establishment']
};
autocomplete = new google.maps.places.Autocomplete(input, options);
autocomplete.bindTo('bounds', map);
var place = autocomplete.getPlace();
var cd= place.geometry.location;
}
But I am getting Error on execution as:
place is undefined.
**(
TypeError: place is undefined
var cd= place.geometry.location;
)**
and I am defining my script as
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script>
Check out https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete for an example of how best to use Autocomplete in the Maps API.
autocomplete.getPlace() will not return a valid Place object until the user has entered one. You can tell when this happens by listening to the place_changed event on the autocomplete object.
google.maps.event.addListener(autocomplete, 'place_changed', function() {
console.log(autocomplete.getPlace());
}
I am using google places autocomplete api and now I want to get the geocode of that address and display that area on map.This is my code...
<head>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>
<script>
// This example adds a search box to a map, using the Google Place Autocomplete
// feature. People can enter geographical searches. The search box will return a
// pick list containing a mix of places and predicted search terms.
function initialize() {
var input = document.getElementById('searchTextField');
var autocomplete = new google.maps.places.Autocomplete(input);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<input id="searchTextField" type="text" size="50">
</body>
I know there is google geocde api but I really dont know how to do that.
I m doing all this in wordpress. Anybody has idea about this???
Looks like that's what you need:
<style>
#map-canvas {
width: 500px;
height: 500px;
}
</style>
Then, the JS
function initialize() {
// initial zoom and position
var myOptions = {
zoom: 14,
center: new google.maps.LatLng(-25.3, 133.8)
};
// create Map instance
var map = new google.maps.Map(document.getElementById('map-canvas'), myOptions);
// get input reference
var input = document.getElementById('searchTextField');
// create instance of autocomplete
var autocomplete = new google.maps.places.Autocomplete(input);
// listen for changes
google.maps.event.addListener(autocomplete, 'place_changed', function() {
// get the selected place
var place = this.getPlace();
// if there's a geometry available
if (place.geometry) {
// move the map to the position
map.panTo(place.geometry.location);
// update the zoom
map.setZoom(15);
// log the position
console.log(place.geometry.location.lat(),place.geometry.location.lng() )
}
})
}
More info: https://developers.google.com/maps/documentation/javascript/places-autocomplete
I've got a Google Maps widget working on an .xhtml page served by a JSF2 project in Java. The map displays, and I have a function for adding markers that works just fine. However, any attempt to use Geocoder seems to result in the returned status of 'ERROR'. Google Maps API mentions no such error status amongst its five possible candidates for return values of the 'status' variable.
Followng is a .js file deployed, and the offending function in question is codeAddress(). Similar code to codeAddress() works elsewhere online that I can find, so it must be something to do with the circumstances under which its being called. Given that the map displays just fine, and I can place markers, my expectation is that my session is just fine. This leaves me puzzled :( Does anyone know what 'ERROR' could come from??
var map;
var marker;
var geocoder;
function placeMarker(location) {
if (!marker) {
marker = new google.maps.Marker({
position: location,
map: map
});
} else {
marker.setPosition(location);
}
map.setCenter(location);
var field = document.getElementById("portCreate:coordinate");
field.value = "POINT (" + location.lat() + " " + location.lng() + ")";
}
function initialize() {
var mapOptions = {
center : new google.maps.LatLng(55.761123, 13.084717),
zoom : 5,
mapTypeId : google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(
document.getElementById("map"), mapOptions);
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
});
setMarker(document.getElementById("portCreate:coordinate"));
geocoder = new google.maps.Geocoder();
}
function setMarker(field) {
if (field.value) {
var regex = /POINT.+?([-0-9\.]+).+?([-0-9\.]+)/;
var matches = regex.exec(field.value);
placeMarker(new google.maps.LatLng(Number(matches[1]), Number(matches[2])));
}
}
function codeAddress(address) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
} else {
alert("Geocode was not successful for the following reason: " + status + "\nFor address " + address);
}
});
}
function loadScript() {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://maps.googleapis.com/maps/api/js?key=<API key here>&sensor=false&callback=initialize";
document.body.appendChild(script);
}
window.onload = loadScript;
After having this same problem today I've remembered that instead of doing a link to the API I downloaded the code as .js. After changing it to a link it works agains. I think Google updates the script regurlaly, if it had any version conflict they return the ERROR status.
The documentation for javascript/geocoding shows 'ERROR' as a possible result:
"ERROR" indicates that the request timed out or there was a problem contacting the Google servers. The request may succeed if you try again.
I had the same problem. I was working with ASP .Net application. After I changed the ASP .Net button control to HTML input Button, it is working now. Not sure where the problem is.
<input type="button" value="Geocode" onclick="codeAddress()">
I was troubleshooting a similar issue, where I was able to recreate the ERROR status by canceling the google API http request before it returned a response.
I've got a setup in which multiple markers present, each loading in content to a single InfoWindow when it's clicked.
Unfortunately, it seems that the marker's click event is not being fired when it's clicked while another window is open.
This seems like an odd behavior. Has anyone else found this to be the case?
Thanks
My code below:
var infoWindow = new google.maps.InfoWindow();
if(markers) {
var length = markers.length;
for(var i = 0; i < length; i++) {
var details = markers[i];
markers[i] = new google.maps.Marker({
title: details.name,
position: new google.maps.LatLng(details.location[0],details.location[1]),
locCode: details.locCode
});
markers[i].setMap(map);
var thisMarker = markers[i];
google.maps.event.addListener(thisMarker, 'click', (function(thisMarker, i, details) {
// self calling function was the key to get this to work
return function() {
$.ajax({
url: 'js/locations/'+details.locCode+'.js',
dataType: 'script',
success: function(data) {
// do my specific stuff here, basically adding html to the info-window div via jQuery
// set the content, and open the info window
infoWindow.setContent($("#info-window").html());
infoWindow.open(map, thisMarker);
}
});
}
})(thisMarker, i, details));
}
}
Check out my answer here:
Jquery Google Maps V3 - Information window is always fixed to one marker
I think this will help resolve your issue.
Bob
Basically I have a KML file that has a TON of polygons to be mapped out. I need those polygons to be clickable in which I'd perform an ajax response.
I'm pretty lost, though. Can someone point me in the right direction? :)
Check out the polygon-array example that the Google Maps documentation refers to. It shows how to draw polygons, and how to respond to clicks on such polygons.
To view the example:
http://code.google.com/apis/maps/documentation/javascript/examples/polygon-arrays.html
To view the source-code behind it:
view-source:http://code.google.com/apis/maps/documentation/javascript/examples/polygon-arrays.html
function initialize() {
var mapOptions = {
zoom: 5,
center: new google.maps.LatLng(41.376259, 25.055542),
mapTypeId: google.maps.MapTypeId.TERRAIN
};
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var Place = new google.maps.KmlLayer({
url: 'path-to-kml.kml'
});
Place.setMap(map);
google.maps.event.addListener(Place, 'click', function (event) {
window.location.href = 'http://example.com'
});
}
google.maps.event.addDomListener(window, 'load', initialize);