I'd like to ask you a question about calculating the distance of Google Map V2's current location <-> marker.

Asked 2 years ago, Updated 2 years ago, 22 views

The progress has been completed by displaying the current location on the map and entering latitude/ longitude in the Eclipse to display the marker.

What I'm curious about is that you calculate the distance from your current location to the nearest marker I'd like to display the result value on the screen.

When I google it, I think there is a way to calculate using distance to, and a way to use Google Maps Directions API.

I wonder how to solve it in order to go in the direction I want.

Please answer it!

android

2022-09-22 08:11

1 Answers

I think you should use Google Maps directions api to show how long it takes with options such as car/walking using Google Maps' directions. Or Simply use the CalculationByDistance method in the following code to obtain it.

import java.text.DecimalFormat;

class CodeRunner{
    public static void main(String[] args){
        LatLng gangnamStation = new LatLng (37.4979462, 127.025427);//Gangnam Station
        LatLng Shin Nonhyun = new LatLng (37.5047282,127.023186);//Shinnonhyeon Station
        System.out.println(CalculationByDistance( gangnamStation, shinNonhyeon));

    }
    public static double CalculationByDistance(LatLng StartP, LatLng EndP) {
        int Radius = 6371;// radius of earth in Km
        double lat1 = StartP.latitude;
        double lat2 = EndP.latitude;
        double lon1 = StartP.longitude;
        double lon2 = EndP.longitude;
        double dLat = Math.toRadians(lat2 - lat1);
        double dLon = Math.toRadians(lon2 - lon1);
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
                + + Math.cos(Math.toRadians(lat1))
                * * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
                * * Math.sin(dLon / 2);
        double c = 2 * Math.asin(Math.sqrt(a));
        double valueResult = Radius * c;
        double km = valueResult / 1;
        DecimalFormat newFormat = new DecimalFormat("####");
        int kmInDec = Integer.valueOf(newFormat.format(km));
        double meter = valueResult % 1000;
        int meterInDec = Integer.valueOf(newFormat.format(meter));

        return Radius * c;
    }
}

class LatLng{
    public double latitude;
    public double longitude;

    public LatLng(double lat, double lng){
        this.latitude = lat;
        this.longitude = lng;
    }
}

Source

It's declared static, but you can use it as you like. I put the LatLng class of the code just to test it in a Java environment.


2022-09-22 08:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.