What's the best way to determine the distance between an object and your current location, and display it in a text format

I need to create a boundary around a specific city on a Google Map, so I developed the following code which is working perfectly.

<!DOCTYPE html>
<html>
<style>
body {
  margin: 0;
  padding: 0;
  font: 12px sans-serif;
}
h1, p {
  margin: 0;
  padding: 0;
}
#map_div{
    width:400px;
    height:400px;
    margin: 0 auto;
}
#outer {
    width:400px;
    height:50px;
    margin:0 auto;
    text-align:center;
}
#outer input[type="text"] {
    display: block;
    margin: 0 auto;
}

</style>
<body>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?v=3&amp;sensor=false"></script>

<div id="outer">
<input type="text" name="location" value="Current Location"><br>
<div id="map_div"></div>
</div>

</body>
<script type="text/javascript">
google.maps.event.addDomListener(window, "load", function () {
    myMap();
});

function myMap() {
        //add global geocoder
    window.geocoder = new google.maps.Geocoder();

  var mapCanvas = document.getElementById("map_div");
  var mapOptions = { zoom: 10, mapTypeId: google.maps.MapTypeId.ROADMAP, center: new google.maps.LatLng(33.808678, -117.918921) };
  window.map = new google.maps.Map(mapCanvas,mapOptions); 

  drawCircleAroundCity('ottawa');
}

function drawCircleAroundCity(cityName){
    if(!cityName) return;
  if(!window.geocoder) throw "Geocoder is not loaded";

  window.geocoder.geocode({ 'address': cityName }, function (results, status) {
    if (status == 'OK') {
      addCircle(results[0].geometry.location, true);
    } else {
      throw 'Unable to find address: ' + address;
    }
  });
}

function addCircle(position, recenter){
    if(typeof(position) != 'object') return;
  if(!window.map) throw "Map is not loaded";

  var myCity = new google.maps.Circle({
  center: position,
  radius: 50000,
  strokeColor: "#0000FF",
  strokeOpacity: 0.8,
  strokeWeight: 2,
  fillColor: "#0000FF",
  fillOpacity: 0.4
  });

  myCity.setMap(window.map);

  if(recenter)
    window.map.setCenter(position);
}
</script>
</html>

Challenge Description:

Now, I have to determine the distance of an item from the current location and display it just above the Current Location textbox as depicted in the image below. The item details I have include the complete address. Can I use this full address to calculate the distance from Current Location and display it above the shown textbox in the image? For example, this is 187.51 km away from. I'm uncertain about the correct approach for this task.

Here is my functioning jsfiddle

{
    "country": "canada",
    "state": "ontario",
    "city": "ottawa",
    "street": "riverside drive",
    "number": "1750",
    "postal": "k1g 3t6"
}

https://i.sstatic.net/gnMIq.png

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

A convenient Delete Modal Component in React utilizing Reactstrap

I am currently working on implementing a reusable Delete Component using reactstrap, which can be called from other components. Below is the code for my DeleteModal: class DeleteModal extends Component { constructor(props) { super(props); this. ...

Error: The checkbox was clicked, but an undefined property (includes) cannot be read

Link to live project preview on CodeSandbox Visit the product page with checkbox I have developed a code snippet that allows users to filter products by checking a box labeled "Show Consignment Products Only", displaying only those products with the term ...

use jquery to increase margin on card upon collapse displaying

I'm currently using an accordion from a bootstrap 4 website. <div class="accordion" id="accordionExample"> <div class="card m-0"> <div class="card-header bg-white" id="headingOne"> <h5 class="mb-0"> <but ...

When you hover over the button, a picture will appear

This is an example code snippet from W3Schools. It showcases an animated button that reveals a small arrow next to the text when hovered over. The specific line of code responsible for this effect is .button span:after {content: '\00bb'. How ...

Using JQuery to load elements and applying Select2 functionality

I'm currently utilizing select2 in a project, and it's functioning properly by initializing the select2 using: $(function() { $(".elegible").select2(); }); The problem arises when I attempt to apply the function to elements loaded through jQ ...

Retrieving the value of a JSON object within a nested response

I need to extract specific values from a JSON response related to cars. Specifically, I want to retrieve the values for car, car.Make, and car.Price. However, my current method does not seem to be working. Here's what I've tried: $.each(jqXHR. ...

How to access elements that are dynamically added using Jquery

Similar Question: dynamic class not triggering jquery unable to select dynamically added element I am facing an issue trying to access a dynamically added element by its class name. Here is a snippet of the HTML code I am working with: <div c ...

Wrapping content in Vue/Vuetify into columns according to screen size

I am working on a parent component that uses a v-for to render instances of the product-box component based on the number of products. Currently, when there are 200 products, they simply stack on top of each other extending the page length like this: [box ...

Learn how to implement the PATCH method specifically for the scenario when the user clicks on the "forgot password"

After clicking on the forgot password link, I want to display the password change form. However, I'm facing issues with calling the API to update the user's password. Below are the files I'm utilizing to develop the API, trigger events, mana ...

Stop the print dialog box from causing the page to refresh

I have implemented a print button for an invoice: </script> <!--Function for printing invoice--> <script> function printpage() { window.print() } </script> <button id="print" name="print" class="btn btn-info" onClick="pri ...

Adjust the map zoom and boundaries for all markers on Google API v3

I have been struggling with implementing latlngbounds to dynamically fit all the pins in the map canvas. If anyone has experience with a similar issue and can provide guidance on where my code might be going wrong, I would greatly appreciate it. var geo ...

The test is failing to execute the service mock promise due to an issue with the `

A problem has arisen while creating a mock for the BoardService. It appears that the .then function is not executing in the controller during testing, even though it works perfectly fine in the live application. Below is the test snippet: beforeEach(inje ...

Properly handling curves in THREE.JS to ensure efficient disposal

I am facing a challenge with managing memory in my project. I have a dynamic sphere with moving points on it, and I am creating curves to connect these points. Here is an illustration of the process: https://i.sstatic.net/PGWuU.jpg As the points are cons ...

RN TypeScript is handling parameters with an implicit any type

My React Native (RN) application includes the following code snippet: handleTextChange = e => { this.setState({ value: e }) } I am using TypeScript (TS) and it's giving me a warning saying, "parameter 'e' implicitly has 'any&apos ...

"I can't seem to get this alignment right. What's going on

Is there a way to adjust the spacing more effectively? When the screen is resized, there is a large gap as highlighted in the red circle. My goal is to have it align correctly with the other divs instead of its current behavior. Thank you! https://i.sstat ...

Having trouble decoding audio data in JavaScript Web Audio?

I'm attempting to utilize the Web Audio API in JavaScript to upload a sound into a buffer and play it. However, I am encountering an issue where it does not work and an error message is displayed: Uncaught TypeError: Failed to set the 'buffer&ap ...

Guide on how to modify the CSS styling of a particular <td> element when its parent contains a specific class

.webgrid-table td, th { border: 1px solid #98bf21; padding: 3px 7px 2px; } I am facing an issue where the style defined above is being applied to all td elements. However, I want to exclude the styling for a td that is within a tr element with the ...

Using a local JSON file's key as a variable within a JavaScript function: a step-by-step guide

I am working on a small system to track sales. The goal is to have the ability to click on a specific item, and both the total value (items sold) and revenue increase accordingly. Currently, I have a button with a JavaScript function called incrementValue ...

Anticipating the information that I will retrieve from the server

Greetings to all! I am still new here, so please bear with me if my layout is not up to par. I have a reactive form where I submit data to the server (mongodb database). After that, I aim to redirect to a new path while passing along the ID element provid ...

next.js does not optimize JSON data files

During the deployment of my application container, I need to substitute a JSON file with a new one. Situation I have developed a next.js application that relies on a particular JSON file for data. Everything works perfectly when the file is imported into ...