My google map is not showing correctly. It shows "for development purposes only" and google says I need to create and enable a billing account and create an API key. I have done this but do I need to change or add something in the code?
// Get the HTML DOM element that will contain your map
// We are using a div with id="map" seen below in the <body>
var mapElement = document.getElementById('map');
var map = new google.maps.Map(mapElement, mapOptions);
var image = 'http://filmservice.no/wp-content/themes/filmservice16/img/ikon/map-marker.png';
var marker = new google.maps.Marker({
position: new google.maps.LatLng(59.969826, 10.905725),
animation: google.maps.Animation.DROP,
map: map,
icon: image,
title: 'Filmservice AS'
});
function toggleBounce() {
if (marker.getAnimation() !== null) {
marker.setAnimation(null);
} else {
marker.setAnimation(google.maps.Animation.BOUNCE);
}
}
The change you'll need to make is actually in your HTML markup, where you include the google maps API script:
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap" async defer></script>
See the tutorial at https://developers.google.com/maps/documentation/javascript/tutorial for more information.
Related
I'm working on a javascript Google map where I have marked the location of each of the state capitals with a custom marker and a title of that capital and state. Right now when you view the map you see the whole US with each marker. I want to keep that but add a sidebar where you click the name of a state and the map zooms to the state and capital. I would also like to add a link that zooms out view the full map. Does anyone have a tutorial on how to do this? I found several examples of Google maps with sidebars but none that zooms to a specific location.
Edit:
This is what I am trying to achieve: http://econym.org.uk/gmap/example_map2.htm
Here is the code I am working with:
<script>
function init() {
var myOptions = {
zoom: 4,
center: new google.maps.LatLng(38.781494, -96.064453),
mapTypeId: google.maps.MapTypeId.ROAD
};
var map = new google.maps.Map(document.getElementById("map"), myOptions);
var image = 'Alabama.png';
var myLatLng = new google.maps.LatLng(32.366805, -86.299969);
var AlabamaMarker = new google.maps.Marker({
position: myLatLng,
map: map,
icon: image,
title:"Montgomery, Alabana"
});
}
google.maps.event.addDomListener(window, 'load', init);
</script>
You need to associate listeners (for example, using jquery) with the click event on your state names. Provided they had an id attribute with the USPS code, it would be something like
jQuery(document).on('click','#RESET',function() {
map.setZoom(5);
map.setCenter({lat:41, lng:-89.5});
});
jQuery(document).on('click','#CA',function() {
map.setZoom(7);
map.setCenter({lat:36.4, lng:-120.9});
});
jQuery(document).on('click','#FA',function() {
map.setZoom(7);
map.setCenter({lat:28, lng:-81});
});
You see, I included one link with id RESET that allows me to reset to the initial state.
Here you can see it at work
http://bl.ocks.org/amenadiel/38e0541592bf331cb298
I am working on a website that have multiple companies and employees, each company should register by entering its username,password and location on Google maps.
The location is going to appear to employees who are using an android application (Which is connected to the same website's database).
I want to know how to allow the company to specify their exact address either by typing the decimal points of the location or by using the pin to specify it on Google map or by allowing me to detect their location.
I read this: https://developers.google.com/maps/documentation/javascript/tutorial
but I do not know if it is what I need in my case.
Note that it is the first time for me to deal with maps so I am not deep into it.
I hope i understand you correctly.
One option could be something like this:
Get the user device' geolocation and put that position on a map with a draggable marker.
If the position is not correct, marker can be dragged and you will get the new coords - if the marker location has been changed.
Here is the code (with jquery):
var map;
var marker;
function initialize() {
var mapOptions = {
zoom: 11
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
// Try HTML5 geolocation
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
var marker = new google.maps.Marker({
position: pos,
map: map,
title: 'Here you are',
draggable: true
});
$('#user-info').append('Location found using HTML5<br/>');
map.setCenter(pos);
google.maps.event.addListener(marker, 'dragend', function(a) {
$('#user-info').append(a.latLng.lat().toFixed(4) + ', ' + a.latLng.lng().toFixed(4) +'<br/>' );
});
}, function() {
handleNoGeolocation(true);
});
} else {
// Browser doesn't support Geolocation
handleNoGeolocation(false);
}
}
function handleNoGeolocation(errorFlag) {
if (errorFlag) {
$('#user-info').append('Error: The Geolocation service failed.');
} else {
$('#user-info').append('Error: Your browser doesn\'t support geolocation.');
}
var options = {
map: map,
position: new google.maps.LatLng(60, 105),
};
map.setCenter(options.position);
}
google.maps.event.addDomListener(window, 'load', initialize);
http://jsfiddle.net/iambnz/xdoc1nxm/
I hope this will help you.
In case you will need to get the exact address, you have to add geocoding functionality.
I have a database of locations which I want to be able to print on a map. Ideally there should be one map with multiple pins for each location you have toggled on. So click a button for location X and it shows up on the map. Click the button for location Y and it shows up on the same map. Click X again and it hides from the map.
Currently I have it so I click on X and the map gets redrawn centered around point X.
Here is the HTML for each button:
<input type='button' data-lat='38.89864400' data-long='-77.05283400'
data-when='20 Aug at 2:00am' value='Location X' class='click' />
The jQuery I'm using is:
jQuery(document).ready(
function initialize() {
jQuery("input.click").click(function() {
showOnMap(jQuery(this).data('lat'), jQuery(this).data('long'), jQuery(this).data('when'));
});
}
);
function showOnMap(lat, long, message) {
var myLatlng = new google.maps.LatLng(lat, long);
var mapOptions = {
zoom: 13,
center: myLatlng
}
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: message
});
google.maps.event.addDomListener(window, 'load', showOnMap);
}
Is there an easy way to switch from what I have to what I want? I've searched for a while but no one seems to be asking this use case in a browser, just Android (which I'm not doing).
Thanks!
There is an example in the documentation on how to hide/show markers. In short, a marker is:
hidden by setting its map to null
showed by setting its map to map
To do so, you will need to access each marker individually. If you have a definite number of locations, it can be done by naming them with different names (eg var markerLocationX, var markerLocationY, etc). Otherwise, the markers need to be stored in an array.
Supposing you have a definite number of known locations to toggle the markers, your javascript code may look like this:
function toggleMarker(markerName) {
if (markerName.getMap() == null) {
markerName.setMap(map);
} else {
markerName.setMap(null);
}
}
I've got an application (WebApp) that is running local (of course). However, I can suggest users might have the app downloaded, but not always have 3G/WiFi when running the app. Therefor, the Google Map would not load when a user doesn't has an internet connection, since it needs the web API.
As a fallback, I would like to show an image ('screenshot') when the map could not be loaded.
What would be the most appropriate solution? Thanks!
The whole map canvas works with help of these code pieces
HTML
<div id="map-canvas"></div>
CSS
#map-canvas {
width:100%;
height:200px;
}
Javascript
function initialize() {
var myLatlng = new google.maps.LatLng(51.81199,4.66656);
var mapOptions = {
zoom: 9,
center: myLatlng
}
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: 'Hello World!'
});
}
google.maps.event.addDomListener(window, 'load', initialize);
// END OF GOOGLE MAPS //
External Google API script
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
You wouldn't use the following line:
google.maps.event.addDomListener(window, 'load', initialize);
On window.onload you would check to see if google.maps exists and then use an if/else statement to toggle between the conditions - initialize, or load the image in the map's place. Something like:
window.onload = function () {
var foundGoogle, img;
foundGoogle = typeof google === 'object' && typeof google.maps === 'object';
if (foundGoogle) {
initialize();
} else {
img = new Image();
img.src = 'screenshot.png';
document.getElementById('map-canvas').appendChild(img);
}
}
well im trying to set the BOUNCE animation to a specific marker but whenever i call the marker.setAnimation(google.maps.Animation.BOUNCE) method console says "Cannot read property 'BOUNCE' of undefined" this means that marker is not defined right? but if I use marker.setTitle('Bouncing') the title does change. am i doing something wrong , here is the code
<script type="text/javascript">
function addMarker(lat,lng,img,title,bounce)
{
var myLatLng = new google.maps.LatLng(lat, lng);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
icon: img,
title: title,
zIndex: 1
});
if(bounce=='set'){marker.setAnimation(google.maps.Animation.BOUNCE);
marker.setTitle('Bouncing');};
}
</script>
php script
for($i=0;$i<count($losDatos);$i++)
{
$utc=new DateTime($losDatos[$i]['fechaUtc']);
$utc->modify('-'.horarioVerano().' hours');
echo $utc->format("Y-m-d H:i:s");
if($losDatos[$i]['camion']==$camion)
{
$script.="addMarker(".$losDatos[$i]['latitud'].",".$losDatos[$i]['longitud'].",".$losDatos[$i]['img'].",".$losDatos[$i]['nombre'].",'set');";
}else
{
$script.="addMarker(".$losDatos[$i]['latitud'].",".$losDatos[$i]['longitud'].",".$losDatos[$i]['img'].",".$losDatos[$i]['nombre'].");";
}
}
echo $script;
try:
marker.setAnimation(google.maps.Animation.BOUNCE)
The way You specified it in you code is correct.
{
marker.setAnimation(google.maps.Animation.BOUNCE);
}
What you should check is if the marker is really referencing a marker object on the map.
OR
You can try setting the animation through marker options.
var markerOptions = {animation:google.maps.Animation.BOUNCE}
or Try setting the animation without the if(condition) to to see if it bounces.
Also please check for equality this way in your if statement
if(bounce==="set"){ /*animate marker*/}
The setAnimation param should be a string of either "BOUNCE" or "DROP".
marker.setAnimation("BOUNCE");
or
marker.setAnimation("DROP");
where marker is a google maps marker object: