Find Location By Latitude And Longitude Google Maps Javascript

Find Location by Latitude and Longitude using Google Maps JavaScript

Here is an example of how you can use Google Maps JavaScript API to find a location by latitude and longitude:


<!DOCTYPE html>
<html>
  <head>
    <title>Find Location by Latitude and Longitude</title>
    <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"></script>
    <script>
      function initialize() {
        var latLng = new google.maps.LatLng(37.7749, -122.4194); // Replace with your desired latitude and longitude
        var mapOptions = {
          zoom: 12,
          center: latLng
        };
        var map = new google.maps.Map(document.getElementById('map'), mapOptions);
        var marker = new google.maps.Marker({
          position: latLng,
          map: map
        });
      }
      google.maps.event.addDomListener(window, 'load', initialize);
    </script>
    <style>
      #map {
        height: 400px;
        width: 100%;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
  </body>
</html>
  

In the above example, we are creating a Google Map using the latitude and longitude provided (37.7749, -122.4194). Replace these values with your desired latitude and longitude.

The map is displayed in the <div id="map"></div> element, which has a height of 400px and width of 100% as defined in the CSS.

The initialize() function is called when the window has finished loading. Inside this function, we create a new LatLng object with the provided latitude and longitude, and set it as the center of the map. We also set the zoom level to 12.

Finally, we create a new marker object and set its position to the provided latitude and longitude. The marker is then displayed on the map using the map property.

Remember to replace YOUR_API_KEY with your actual Google Maps API key, which you can obtain from the Google Cloud Platform Console.

Leave a comment