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 can I get the y position of an element using jQuery?
- How can I use JQuery with Yii2?
- How do I use the jQuery masked input plugin?
- How do I uncheck a checkbox using jQuery?
- How can I convert jQuery code to vanilla JavaScript?
- How can I convert XML data to JSON using jQuery?
- How can I use jQuery to control the visibility of an element?
- How do I use the jQuery UI Datepicker?
- How do I use jQuery to trigger an event?
- How do I use jQuery to toggle an element?
See more codes...