6

Google マップ上の 2 点間の距離を計算できる小さな Web アプリがあります。

アプリをロードするときに、ユーザーを現在の位置に配置したいと思います。さまざまな地理位置情報方法を試してみましたが、うまくいきませんでした。

最良の方法は、ユーザーの位置からの距離を計算することです。ただし、Web アプリでユーザーの位置を設定するだけで十分です。

<html>
    <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript">

    var directionDisplay;
    var map;


function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();
    var copenhagen = new google.maps.LatLng(55.6771, 12.5704);
    var myOptions = {
        zoom:12,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: copenhagen
    }

    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    directionsDisplay.setMap(map);
    }


    var directionsService = new google.maps.DirectionsService();

    function calcRoute() {
    var start = document.getElementById("start").value;
    var end = document.getElementById("end").value;
    var distanceInput = document.getElementById("distance");

    var request = {
        origin:start,
        destination:end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };

    directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
            distanceInput.value = response.routes[0].legs[0].distance.value / 1000;
        }
    });
    }
    </script>

    <title>Distance Calculator</title>

    <style type="text/css">

            body {
                font-family:Helvetica, Arial;
            }
            #map_canvas {
                height: 50%;
            }
    </style>
    </head>
    <body>

    <body onload="initialize()">
    <p>Enter your current location and desired destination to get the distance</p>
        <div>
            <p>
                <label for="start">Start: </label>
                <input type="text" name="start" id="start" />

                <label for="end">End: </label>
                <input type="text" name="end" id="end" />

                <input type="submit" value="Calculate Route" onclick="calcRoute()" />
            </p>
            <p>
                <label for="distance">Distance (km): </label>
                <input type="text" name="distance" id="distance" readonly="true" />
            </p>
        </div>
        <div id="map_canvas"></div>
    </body>
</html>
4

2 に答える 2

1

このリンクを試す必要があります

google.maps.LatLng.prototype.distanceFrom = function(newLatLng) {
// setup our variables
var lat1 = this.lat();
var radianLat1 = lat1 * ( Math.PI  / 180 );
var lng1 = this.lng();
var radianLng1 = lng1 * ( Math.PI  / 180 );
var lat2 = newLatLng.lat();
var radianLat2 = lat2 * ( Math.PI  / 180 );
var lng2 = newLatLng.lng();
var radianLng2 = lng2 * ( Math.PI  / 180 );
// sort out the radius, MILES or KM?
var earth_radius = 3959; // (km = 6378.1) OR (miles = 3959) - radius of the earth

// sort our the differences
var diffLat =  ( radianLat1 - radianLat2 );
var diffLng =  ( radianLng1 - radianLng2 );
// put on a wave (hey the earth is round after all)
var sinLat = Math.sin( diffLat / 2  );
var sinLng = Math.sin( diffLng / 2  ); 


var a = Math.pow(sinLat, 2.0) + Math.cos(radianLat1) * Math.cos(radianLat2) * Math.pow(sinLng, 2.0);

// work out the distance
var distance = earth_radius * 2 * Math.asin(Math.min(1, Math.sqrt(a)));

// return the distance
return distance;
}

distance-finder-google-maps-api

于 2012-10-16T09:23:20.200 に答える
1

javascript を使用してユーザーの位置を取得するには:

var showPosition = function (position) {
    var userLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
    // Do whatever you want with userLatLng.
}
navigator.geolocation.getCurrentPosition(showPosition); // Attach showPosition Callback which will be executed as soon as position is available. Remember it needs user confirmation.

たとえば、http://jsfiddle.net/Stouffi/FCFSW/を参照してください。

アップデート:

calcRoute ボタンの onclick コールバックを変更する必要があります。新しいものはユーザーの位置を要求します。位置が取得されると、calcRoute が呼び出されます。

function findCurrentLocation() {
    navigator.geolocation.getCurrentPosition(calcRoute);
}

calcRoute では、位置オブジェクトを引数として使用できます。これを LatLng オブジェクトに変換して、Google ルート サービスを処理する必要があります。

function calcRoute(currentLocation) {
    // create a LatLng object from the position object.
    var start = new google.maps.LatLng(currentLocation.coords.latitude, currentLocation.coords.longitude);
    // Continue with your original code.
    var end = document.getElementById("end").value;
    // etc.

http://jsfiddle.net/Stouffi/mCYTf/を参照してください

于 2012-10-16T09:33:42.460 に答える