Get nearby Lat Long in google map

Sample code for Google Maps Platform JavaScript API author(s): Justin Poehnelt

by Faisal Khan Janjua

HTML

<!DOCTYPE html>
<!--
 @license
 Copyright 2019 Google LLC. All Rights Reserved.
 SPDX-License-Identifier: Apache-2.0
-->
<html>
  <head>
    <title>Simple Map</title>
    <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
    <!-- jsFiddle will insert css and js -->
  </head>
  <body>
    <div id="map"></div>

    <!-- 
     The `defer` attribute causes the callback to execute after the full HTML
     document has been parsed. For non-blocking uses, avoiding race conditions,
     and consistent behavior across browsers, consider loading using Promises
     with https://www.npmjs.com/package/@googlemaps/js-api-loader.
    -->
    <script
      src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap&v=weekly"
      defer
    ></script>
  </body>
</html>

CSS

/**
 * @license
 * Copyright 2019 Google LLC. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */
/* 
 * Always set the map height explicitly to define the size of the div element
 * that contains the map. 
 */
#map {
  height: 100%;
}

/* 
 * Optional: Makes the sample page fill the window. 
 */
html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}

JavaScript

let map;

var latlng = getNearByLatLng(38.978934, 33.842368);

function initMap() {
  map = new google.maps.Map(document.getElementById("map"), {
    center: latlng,
    zoom: 20,
    mapTypeId: google.maps.MapTypeId.SATELLITE
  });
  var marker = new google.maps.Marker({
    position: new google.maps.LatLng(latlng.lat, latlng.lng),
    map: map,
  });
}

window.initMap = initMap;




function getNearByLatLng(lat, lng, realponts){
    var r = 0.5/111300; // 0.5 mans 1 meter
    var y0 = parseFloat(lat);
    var x0 = parseFloat(lng);
    var u = 0.5; // Math.random(); use random to get ramdom lat long with in specific range like 1 meter
    var v = 0.5; // Math.random(); use random to get ramdom lat long with in specific range like 1 meter
    var w = r * Math.sqrt(u);
    var t = 2 * Math.PI * v;
    var x = w * Math.cos(t);
    var y1 = w * Math.sin(t);
    var x1 = x / Math.cos(y0);
    var newY = parseFloat((y0 + y1).toFixed(7));
    var newX = parseFloat((x0 + x1).toFixed(7));
    if(realponts){ return { "lat": lat, "lng": lng }; }
    return { "lat": newY, "lng": newX };
}