Posted in js
708
6:20 am, August 31, 2018
 

google map with overlay data

<p class="lead">Ever wanted to add a <del>really</del> slightly complex google map to your site?</p>

<h2>Working Example</h2>

<style>
/ Always set the map height explicitly to define the size of the div
element that contains the map. /
#map {
height: 400px;
}
/
Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>

<div id="map"></div>
<script>

// This example displays a marker at the center of Australia.
// When the user clicks the marker, an info window opens.

function initMap() {
var cairns = {lat: -16.9231209, lng: 145.7690145};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: cairns
});

var contentString = '<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
''+
'<div id="bodyContent">'+
'<p><b>Cairns</b>, The City of Cairns was a local government area centred on the ' +
'Far North Queensland city of Cairns. Established in 1885, for '+
'most of its existence it consisted of approximately 51.5 '+
'square kilometres <a href="https://en.wikipedia.org/wiki/CityofCairns" target="blank">wikipedia</a>'+
'</p>'+
'</div>'+
'</div>';

var infowindow = new google.maps.InfoWindow({
content: contentString
});

var marker = new google.maps.Marker({
position: cairns,
map: map,
title: 'Cairns (City)'
});
marker.addListener('click', function() {
infowindow.open(map, marker);
});
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAvCBl-k4
jpfGnu0TxdPhS7NylxwKQLaw&callback=initMap">
</script>

<br>

<p>To run this you will need your own API key to use the google maps. You can <a href='https://developers.google.com/maps/documentation/javascript/get-api-key' target='blank'>request one from here</a>.</p>

<pre>&lt;script async defer<br /> src="https://maps.googleapis.com/maps/api/js?key=<a href='https://developers.google.com/maps/documentation/javascript/get-api-key' target='blank'>[getkey]</a>&amp;callback=initMap"&gt;<br /> &lt;/script&gt;</pre>

<h2>Get your co-ordinates</h2>

<p>To get the lat and long you will need for the map you will need to go to <a href='http://www.google.com/local' target='blank'>maps.google.com</a> and find the location you want to add a pin.</p>

<p>Search for the palace you want to pin</p>

<p>Up in the URL it will have something like this: <code>@-16.9231209,145.7690145,16z</code> these are your lat and long co-ordinates depending on how accurate you want these you can shorten them a little bit to something like this: -16.923 145.769. Actually scrap that you need the whole lot in there for it to be accurate. </p>

<p>Now we know where to drop the pin.</p>

<pre>var cairns = {lat: -16.9231209, lng: 145.7690145};</pre>

<p>Set the Zoom level 1 is all the way out, and ~22 is all the way in.</p>

<pre>var map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: cairns
});</pre>

<p>Add the popup window content</p>

<pre>var contentString = '&lt;div id="content"&gt;'+<br /> '&lt;div id="siteNotice"&gt;'+<br /> '&lt;/div&gt;'+<br /> '&lt;h1 id="firstHeading" class="firstHeading"&gt;Cairns&lt;/h1&gt;'+<br /> '&lt;div id="bodyContent"&gt;'+<br /> '&lt;p&gt;&lt;b&gt;Cairns&lt;/b&gt;, The City of Cairns was a local government area centred on the ' +<br /> 'Far North Queensland city of Cairns. Established in 1885, for '+<br /> 'most of its existence it consisted of approximately 51.5 '+<br /> 'square kilometres &lt;a href="<a href="https://en.wikipedia.org/wiki/CityofCairns">https://en.wikipedia.org/wiki/CityofCairns</a>"&gt;wikipedia&lt;/a&gt;'+<br /> '&lt;/p&gt;'+<br /> '&lt;/div&gt;'+<br /> '&lt;/div&gt;';</pre>

<p>Add marker and click listener</p>

<pre>var marker = new google.maps.Marker({<br /> position: cairns,<br /> map: map,<br /> title: 'Cairns (City)'<br /> });<br /> marker.addListener('click', function() {<br /> infowindow.open(map, marker);<br /> });</pre>

<p>Thats pretty much it. Find the co-ordinates, add a content and marker, reload and all done. The only issue is you have to do this for each pin and content window that you add, so it can be quite a process to add a bunch of these windows and pins. </p>

<h2>Related Links</h2>
<ul>
<li><a href='https://developers.google.com/maps/documentation/javascript/examples/infowindow-simple' target='
blank'>info window simple</a></li>
<li><a href='https://developers.google.com/maps/documentation/javascript/examples/icon-complex' target='blank'>icon complex</a></li>
<li><a href='https://jsfiddle.net/api/post/library/pure/' target='
blank'>JSFiddle Map Example</a></li>
<li><a href='https://developers.google.com/maps/documentation/javascript/get-api-key' target='_blank'>Get Google Maps API Key</a></li>
</ul>

View Statistics
This Week
334
This Month
1588
This Year
659

No Items Found.

Add Comment
Type in a Nick Name here
 
Other Items in js
modal no libs just js and css jqueryui date selector with examples and code with custom date formatting fancybox youtube showing video links in a lightbox - updated with fixed code parts fix for Uncaught (in promise) Error: reCAPTCHA placeholder element must be empty enable tinymce on a target textarea by id Generate Random Whole Numbers with JavaScript Function Generate Random Fractions with JavaScript jquery document ready make clickable element with clickable class using getElementById and innerHTML to change the html of content no jquery preserve tabs in textarea when tab key is pressed jqueryui includes css and js using vue and json data jqueryui date selector with examples and code jquery accordion fancybox youtube showing video links in a lightbox jquery jqueryui vue script includes get select option form value with jquery show the year with js jquery clone and append elements stacktable jQuery plugin for stacking tables on small screens load content with jquery find all elements add class jquery digital clock with jquery using regex with replace to replace all instances of something in a string random string generator guid set and check a cookie using js cookie validate email address from string check length of element jquery access hacker news json firebase api via jquery testing chartjs clipboard copy js tiny mce editor tinymce do something later with settimeout or loop with setinterval validate form data using javascript to check required html elements mithril testing parallax js scroll testing change the water colour in google maps for an already initialised map scrollbar replacement simplebar load google sheet data into json string with jquery change favicon with jquery add this Slick Slider Carousel change the window title flems embed in url detect window scroll position jquery truncate string using jquery round number with js google map with overlay data
Related Search Terms
Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

You could also follow me on twitter. I have a couple of youtube channels if you want to see some video related content. RuneScape 3, Minecraft and also a coding channel here Web Dev.

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote

minecraft
Latest News
## 🚀 AI Giants Hit Bullseye: Anthropic & OpenAI Achieve Product-Market Fit Anthropic and OpenAI have reached a significant milestone, finding product-market fit with their AI technologies, which means their products effectively meet the needs of their customers, driving growth and adoption. This achievement showcases the practical value of their innovations, enabling businesses and individuals to leverage AI for enhanced productivity and efficiency. With this alignment of product and market needs, these companies are poised to transform industries and shape the future of technology.