jqueryHow can I use jQuery to create a map?
jQuery can be used to create a map by using the Google Maps API. The code below creates a basic map with a marker on it:
<div id="map" style="width:400px;height:400px;background:yellow"></div>
<script>
$(document).ready(function(){
function initMap() {
var myLatLng = {lat: -25.363, lng: 131.044};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: myLatLng
});
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: 'Hello World!'
});
}
});
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
</script>
This code will produce a map with a marker at the coordinates provided. To use this example, the user must first obtain an API key from Google.
The code consists of several parts:
-
The first part is the div with an id of "map" and a width and height of 400px. This is the element that will contain the map.
-
The second part is the jQuery code that is called when the document is ready. This code creates a new google.maps.Map object and passes in the div element as the first parameter. It also sets the zoom level and center of the map.
-
The third part is the code that creates the marker. This code creates a new google.maps.Marker object and passes in the position and map as parameters.
-
The fourth part is the script tag that loads the Google Maps API. This script tag must include the user's API key as a parameter.
Finally, the code will produce a map with a marker at the coordinates provided.
Helpful links
More of Jquery
- How do I use jQuery ZTree to create a hierarchical tree structure?
- How can I use JQuery with Yii2?
- How do I create a jQuery Yes/No dialog?
- How can I get the y position of an element using jQuery?
- How do I use jQuery to zoom in or out on an element?
- How do I use jQuery with Yarn?
- How do I use jQuery to zip files?
- How do I download a zip file using jQuery?
- How do I get the y-position of an element using jQuery?
- How do I use jQuery to detect window resize events?
See more codes...