Jquery谷歌地图用户位置
Jquery Google Maps User location
我有下一个脚本:
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;
}
});
}
我想计算这两个点之间的公里数,结束将是输入值,开始将是用户位置,我找不到任何在线解决方案。什么是谷歌api syntx的用户定位方向服务?我需要前哨将在公里的
Ofir,
您的第一部分代码非常好。在示例中,我使用了$( document ).on("ready", function() ...
而不是$( document ).on( "pageinit", "#calc", function()
启动,因为我不知道您的html结构。
为使其工作所做的更改:我存储调用calcRoute
时的初始位置,其格式可以由Directionneneneba API进一步使用:latitude, longitude
。下面是$("#start").val(latlng.k + ", " + latlng.B);
行:
function drawMap(latlng) {
var myOptions = {
zoom: 10,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true
};
// Log position in start input field
$("#start").val(latlng.k + ", " + latlng.B);
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!"
});
}
然后为了测试,我添加了一个调用calcRoute
:的按钮
$("#calcBtn").click(function() {
calcRoute();
});
并添加了错误处理,以始终获得响应并了解发生了什么:
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
var distanceInput = document.getElementById("distance");
console.log(response);
distanceInput.value = response.routes[0].legs[0].distance.value / 1000;
} else {
alert("Destination not found, error status: " + status);
}
});
您可以在jsfiddle上进行测试:http://jsfiddle.net/gxjfnmdb/4/
相关文章:
- 获取用户位置并将坐标保存在数据库中
- 防止用户在jQuery Mobile中ajax加载页面时单击其他位置
- 使用地理坐标,对更靠近用户的对象进行排序'的位置
- 询问用户是否要打开地理位置
- 有人知道有没有jquery插件可以在图像上写文本,并让用户将其放置在图像内的任何位置
- 一个html/javascript'小工具'知道用户何时单击了小部件外的任意位置
- 当用户单击按钮(在光标位置)时,在输入字段中添加一个文本字符串
- 谷歌地图/GMAP3 - 绘制从用户地理位置到已知目的地的路线 - 需要帮助
- 谷歌地图 - 用户选择不允许位置
- Chrome/Tampermonkey 用户脚本存储在文件系统上的什么位置
- Phonegap通过WiFi和移动网络而非GPS获取用户位置
- 在谷歌地图上显示用户当前位置并更新
- 如何在不使用地理位置的情况下获得用户所在城市
- 如何检测用户何时在Google Places字段中输入无效位置
- 如何设置用户当前位置以初始化离子框架中的函数
- 谷歌地图赢得'当用户拒绝共享位置时,t初始化
- 谷歌地图:使用自定义标记显示当前用户位置
- Cordova地理位置,等待用户权限
- 如何在文本框中缩进文本和光标的起始位置(用户、密码等)
- 如何使用HTML5地理位置javascript初始化带有地理位置(用户的当前位置)的Google Maps API