[英]Combine Google Maps API with geolocation
I wanted to make it so onload my google maps shows a route from wherever I am (through geolocation) to the destination.我想让它加载我的谷歌地图显示从我所在的任何地方(通过地理定位)到目的地的路线。 I have a snippet where it sets a route between 2 points + 1 snippet where the function grabs my current coordinates.
我有一个片段,它在 2 点 + 1 个片段之间设置了一条路线,其中 function 获取了我当前的坐标。
Issue: I don't understand how I can merge these together to get the desired result.问题:我不明白如何将它们合并在一起以获得所需的结果。
Here's the code I'm working with.这是我正在使用的代码。 Any help is appreciated.
任何帮助表示赞赏。 (tho in these demos getting cordinates won't work, it does work in my vsc.
(虽然在这些演示中获取坐标是行不通的,但它在我的 vsc 中确实有效。
//To get user coordinates var myLocation = document.getElementById("XYZ"); function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); } else { myLocation.innerHTML = "Geolocation is not supported by this browser."; } } function showPosition(position) { myLocation.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude; } // Google Maps var directionsDisplay; var directionsService = new google.maps.DirectionsService(); function initMap() { directionsDisplay = new google.maps.DirectionsRenderer(); var porto = new google.maps.LatLng(41.1579416, -8.6257744); var mapOptions = { zoom: 10, center: porto} var map = new google.maps.Map(document.getElementById('map'), mapOptions); directionsDisplay.setMap(map); calcRoute(map); } function calcRoute(map) { var start = new google.maps.LatLng(41.1579416, -8.327744); var end = new google.maps.LatLng(41.1579416, -8.6257744);//linha = localização no porto var startMark = new google.maps.Marker({ position: start, map: map, title: "start" }); var endMark = new google.maps.Marker({ position: end, map: map, title: "end" }); var request = { origin: start, destination: end, travelMode: 'DRIVING' }; directionsService.route(request, function(response, status) { if (status == 'OK') { directionsDisplay.setDirections(response); } else { alert("directions request failed, status="+status) } }); } google.maps.event.addDomListener(window, "load", initMap);
<script src="https://maps.googleapis.com/maps/api/js?libraries=places&key="></script> <div id="map"style="height:200px"></div> <button id="XYZ" onclick="getLocation()">gib coordinates</button>
The geolocation service is asynchronous.地理定位服务是异步的。 Call the
calcRoute
function inside the geolocation callback function, passing in the position returned once it returns the response:在地理定位回调 function 中调用
calcRoute
function,传入返回响应后返回的 position:
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, errorHandler);
} else {
myLocation.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
myLocation.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
var currentPosition = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
calcRoute(map, currentPosition);
}
and for robustness, call it with a default start location (or error message) in the error handler.为了稳健性,在错误处理程序中使用默认起始位置(或错误消息)调用它。
function errorHandler(err) {
console.warn('ERROR(' + err.code + '): ' + err.message);
var defaultPosition = new google.maps.LatLng(41.1579416, -8.327744);
calcRoute(map, defaultPosition);
};
live example, with fix for locations in USA 现场示例,修复了美国的位置
code snippet:代码片段:
//To get user coordinates var myLocation = document.getElementById("XYZ"); function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition, errorHandler); } else { myLocation.innerHTML = "Geolocation is not supported by this browser."; } } function errorHandler(err) { console.warn('ERROR(' + err.code + '): ' + err.message); var defaultPosition = new google.maps.LatLng(41.1579416, -8.327744); calcRoute(map, defaultPosition); }; function showPosition(position) { myLocation.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude; var currentPosition = { lat: position.coords.latitude, lng: position.coords.longitude }; calcRoute(map, currentPosition); } // Google Maps var directionsDisplay; var directionsService = new google.maps.DirectionsService(); var map; function initMap() { directionsDisplay = new google.maps.DirectionsRenderer(); var porto = new google.maps.LatLng(41.1579416, -8.6257744); var mapOptions = { zoom: 10, center: porto } map = new google.maps.Map(document.getElementById('map'), mapOptions); directionsDisplay.setMap(map); } function calcRoute(map, currentPosition) { var end = new google.maps.LatLng(41.1579416, -8.6257744); //linha = localização no porto var startMark = new google.maps.Marker({ position: currentPosition, map: map, title: "start" }); var endMark = new google.maps.Marker({ position: end, map: map, title: "end" }); var request = { origin: currentPosition, destination: end, travelMode: 'DRIVING' }; directionsService.route(request, function(response, status) { if (status == 'OK') { directionsDisplay.setDirections(response); } else { alert("directions request failed, status=" + status) } }); } google.maps.event.addDomListener(window, "load", initMap);
#map { height: 90%; } html, body { height: 100%; margin: 0; padding: 0; }
<script src="https://maps.googleapis.com/maps/api/js?libraries=places&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script> <div id="map"></div> <button id="XYZ" onclick="getLocation()">get coordinates</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.