JSFiddle - React, Tailwind, and code Playground
by Lawrence Ross
HTML
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places&ext=.js"></script>
<select id="location">
<option>Dubai</option>
<option>Sharjah</option>
</select>
<div id="map_canvas"></div>
CSS
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
JavaScript
var geocoder;
var map;
var geoMarker;
function initialize() {
var map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(37.4419, -122.1419),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
geoMarker = new google.maps.Marker();
geoMarker.setPosition(map.getCenter());
geoMarker.setMap(map);
$("#location").change(function() {
var addr = ($('#location').val());
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address': addr
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
geoMarker.setOptions({
position: results[0].geometry.location,
});
} else {
alert("Something got wrong " + status);
}
});
});
}
google.maps.event.addDomListener(window, "load", initialize);