나는 다음 스크립트가 있습니다jQuery를 구글지도 사용자 위치
var directionDisplay;
$(document).on("pageinit", "#calc", function() {
directionsDisplay = new google.maps.DirectionsRenderer();
var defaultLatLng = new google.maps.LatLng(34.0983425, -118.3267434); // Default to Hollywood, CA when no geolocation support
if (navigator.geolocation) {
function success(pos) {
// Location found, show map with these coordinates
drawMap(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));
}
function fail(error) {
drawMap(defaultLatLng); // Failed to find location, show default map
}
// Find the users current position. Cache the location for 5 minutes, timeout after 6 seconds
navigator.geolocation.getCurrentPosition(success, fail, {maximumAge: 500000, enableHighAccuracy:true, timeout: 6000});
} else {
drawMap(defaultLatLng); // No geolocation support, show default map
}
function drawMap(latlng) {
var myOptions = {
zoom: 10,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
// Add an overlay to the map of current lat/lng
directionsDisplay.setMap(map);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: "Greetings!"
});
}
});
var directionsService = new google.maps.DirectionsService();
function calcRoute() {
var start = document.getElementById("start").value;
var end = document.getElementById("end").value;
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);
var distanceInput = document.getElementById("distance");
distanceInput.value = response.routes[0].legs[0].distance.value/1000;
}
});
}
나는이 두 점 사이의 거리의의를 계산하려면를 최종 입력 값이됩니다 그리고 난 찾을 수 없습니다 시작은 을 사용자 gelocation 될 것입니다 그것을위한 온라인 솔루션. 길 찾기 서비스의 사용자 위치에 대한 Google API 구문은 무엇입니까? 나는 코드의
첫 번째 부분은 상당히 좋았다는 outpot는 km의
먼저지도를 그리고 사용자 위치를 얻은 다음 geolocation 코드의 성공 콜백에'calcRoute'를 사용하십시오. 그렇게하면 사용자의 위치를 전달할 수 있습니다. – Andy
원점을 정의하는 방법 : LatLng? "LatLng undefined"오류가 발생합니다. –
ive가 내 코드를 업데이트했습니다. –