Determining the distance from the current location enabled by "setMyLocationEnabled" to a marker placed on the Google Map

I've been working on developing an Android application that can display the distance between two points on Google Maps. The first point is my current location, and the second point is a marker set on the map.

So far, I've successfully implemented setting my location on the map using setMyLocationEnabled(true). Now, I need to figure out how to compare it with a marker I've added to the map at this location:

map.addMarker(newMarkerOptions().position(LOCATION_BEIJING).title("Find me here!"));

Answer №1

If you want to incorporate the Location class into your project, check out the documentation here. Start by setting the location of the marker:

Location markerLoc = new Location("Marker");
markerLoc.setLatitude(marker.latitude);
markerLoc.setLongitude(marker.longitude);

You can refer to this guide on how to retrieve the current location using OnMyLocationChangeListener, and then set it as your Current Location:

Location currentLoc = new Location("Current");
currentLoc.setLatitude(location.latitude);
currentLoc.setLongitude(location.longitude);

After that, utilize the Location class' distanceTo method to calculate the distance between the two locations in meters like so:

Float distance = currentLoc.distanceTo(markerLoc);

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

Is there a straightforward method to retrieve all information from Vue.js?

Is there a way to extract all of this data? I've attempted using this._data.forEach but it doesn't seem to be effective. Thank you! data() { return { childData: '', credit: '', company: '', email: ...

I want to trigger the opening and closing of an accordion by clicking on an arrow

I am encountering an issue with the Material UI accordion. When I click on the arrow, the accordion opens but clicking again does not close it. I would like to make it so that when the user clicks on the arrow, the accordion will toggle between open and cl ...

The absence of libproject in Google Play services (Rev. 30) cannot be found within Eclipse Juno

After updating Google Play services, I encountered a problem where I could not find any libproject within it. When I cleaned the project, my apk file turned into a jar file. Upon further inspection, I realized that there was no libproject available to add ...

Error: An unregistered adapter is causing an issue in reading data. Unknown typeId encountered: 34. Check if you have registered all adapters properly. (Flutter)

Having trouble with app restarting after saving data using hive on disk. I keep getting the error mentioned above and the app fails to open. ...

Utilizing FullCalendar for showcasing comprehensive details

I'm a big fan of the fullcalendar plugin and all its amazing features. I want to enhance it by displaying more customized information for each event when in agendaWeek or agendaMonth view. Switching between views is easy, but I need help filtering out ...

Test an express + sequelize server using chai-http ping command

Currently facing challenges while setting up tests using Express and Sequelize. The testing framework being used is Mocha + Chai. Initially, only a ping test is being attempted. The code snippet from server.js: const express = require('express&apos ...

Executing JavaScript functions within static files in Django

I can't figure out why I'm unable to invoke a javascript function when clicking my Bootstrap button in a Django project. In my directory, I have set up the static folder as "main/static/main/js/script.js". While I can successfully load the stati ...

Troubleshooting IE Freezing Issue Due to JavaScript Code with .addClass and .removeClass

Need some help troubleshooting why my code is causing IE to crash. After commenting out sections, I discovered that the issue arises when using .addClass and/or .removeClass inside the if conditions: if ( percentExpenses > 50 && percentExpenses ...

Is it feasible to verify for vacant dates with a single click?

Is there a way to determine if a date value is empty, and if it is, display a popup indicating so? After some research, I stumbled upon a similar issue where the date value was always filled with a default "mm/dd/yyyy" value. The solution provided involv ...

tips for successfully encoding JSON for an HTTP POST request

After encoding all the parameters using jsonEncode, I will proceed with the HTTP POST request. The source code provided below explains this: var abc = jsonEncode({ "requisitionID": "x", "RetailerTitle": " ...

The component is failing to detect that it is being exported

Encountering a strange error here. I exported the Home component in the usual way. See below for the relevant .js files to fix this error. However, the iOS simulator keeps showing the following error: Element type is invalid: expected a string (for built- ...

Is Cognito redirect causing issues with Angular router responsiveness?

When employing social login via AWS Cognito, Cognito sends a redirect to the browser directing it to the signin redirect URL after signing in. In this case, the specified URL is http://localhost:4200/home/. Upon receiving this redirect, the application in ...

Adding items to an array within a jQuery each loop and performing a jQuery ajax request

I am trying to loop through an array, push the results into a JavaScript array, and then access the data outside of each loop and AJAX call. Can anyone explain how to do this? This is what I have attempted: var ides = ["2254365", "2255017", "2254288", ...

Ensuring the checkbox is disabled prior to editing

Check out my table below: https://i.stack.imgur.com/7byIa.png Whenever I click the edit button, I can modify the status field and action field. This feature works correctly. However, the issue is that I am able to change the values of status and action e ...

Validate the Ajax form upon submission

Is there a way to incorporate code within an AJAX block to validate form fields? Before sending the AJAX request page described below, I need to ensure that the fields for firstname, lastname, and email are filled out. If any of these fields are empty, th ...

Guide to implementing a click-triggered notification using JavaScript

Can anyone assist me with a JavaScript issue regarding applying toast notifications? I need guidance on implementing an onClick toast using JavaScript. The toast should only appear when a specific condition is false; if the condition is true, the page sho ...

What steps can I take to ensure the browser only shows the page once it has completely loaded?

I find it frustrating when webpages load slowly and you can see the process happening. In my opinion, it would be more satisfying to wait until everything is fully loaded - scripts, images, and all - before displaying the page. This brings up two questio ...

Tips for transferring JSON data to a CodeIgniter view

I have a query regarding how to replace a specific section of my webpage with data returned from the controller. Currently, I have a page that displays movie details directly fetched from the database. My goal is to only display movies of a particular genr ...

Another option could be to either find a different solution or to pause the loop until the

Is it possible to delay the execution of a "for" loop until a specific condition is met? I have a popup (Alert) that appears within the loop, prompting the user for confirmation with options to Agree or Cancel. However, the loop does not pause for the co ...

Creating an .aab using Buildozer in Docker: A comprehensive guide

Exciting news - support for AAB files has been added to Python for Android (p4a). With the requirement for new apps to publish with Android App Bundle on Google Play starting from August 2021, this is a significant update for Python developers working on A ...