如何结合地理位置和海拔在谷歌地图
How to combine Geolocation and Elevation in Google Maps
我很难将谷歌地图中的地理位置和海拔结合起来。我需要显示当前位置的海拔高度。pos变量中的值似乎不等于event.latLng.
Google Maps Elevation
这是我的代码试图结合地理位置和海拔
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
// disableDefaultUI: true,
mapTypeId: google.maps.MapTypeId.SATELLITE,
zoom: 20
});
var elevator = new google.maps.ElevationService;
var infoWindow = new google.maps.InfoWindow({map: map});
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
displayLocationElevation(pos, elevator, infowindow);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn''t support geolocation.');
}
function displayLocationElevation(location, elevator, infowindow) {
map.setCenter(location);
Initiate the location request
elevator.getElevationForLocations({
'locations': [location]
}, function(results, status) {
infowindow.setPosition(location);
if (status === google.maps.ElevationStatus.OK) {
// Retrieve the first result
if (results[0]) {
// Open the infowindow indicating the elevation at the clicked position.
infowindow.setContent('The elevation at this point <br>is ' +
results[0].elevation + ' meters.');
} else {
infowindow.setContent('No results found');
}
} else {
infowindow.setContent('Elevation service failed due to: ' + status);
}
});
}
UPDATE:似乎pos变量和事件。latLng有不同的值。pos生成[Object Object] while事件。latLng生产(后期价值,长期价值)。虽然使用pos.lat生成纬度,但与使用pos.long生成经度相同。
我知道了!下面是代码:
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
// disableDefaultUI: true,
mapTypeId: google.maps.MapTypeId.SATELLITE,
zoom: 20
});
var elevator = new google.maps.ElevationService;
var infoWindow = new google.maps.InfoWindow({map: map});
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
map.setCenter(pos);
displayLocationElevation(map.getCenter(), elevator, infoWindow);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn''t support geolocation.');
}
function displayLocationElevation(location, elevator, infoWindow) {
elevator.getElevationForLocations({
'locations': [location]
}, function(results, status) {
infoWindow.setPosition(location);
if (status === google.maps.ElevationStatus.OK) {
if (results[0]) {
infoWindow.setContent('The elevation at this point <br>is ' +
results[0].elevation + ' meters in ' + location);
} else {
infoWindow.setContent('No results found');
}
} else {
infoWindow.setContent('Elevation service failed due to: ' + status);
}
});
}
希望对别人有所帮助!:)
相关文章:
- 谷歌地图固定位置覆盖
- 如何创建一个谷歌地图地理坐标数组
- 为什么无法根据谷歌地图地理编码geometry.bounds找到位置,但可以通过MongoDB中的geometry.vi
- javascript/谷歌地图:将位置对象转换为字符串
- 用城市名称(地址?)设置谷歌地图标记位置
- Wordpress中的谷歌地图地理编码不起作用
- 替代谷歌地图地理定位API
- 同步请求谷歌地图地理编码
- 谷歌地图的位置图钉标记可以生成邻域名称
- 如何将位置从窗口手机7的观察者转换为谷歌地图的位置
- 返回英国邮政编码只是谷歌地图地理编码结果的一部分
- 谷歌地图地理编码器,计算距离,
- 取消之前对谷歌地图地理编码方法的调用
- 两个网络组件之间的绑定:谷歌地图+地理定位
- 将谷歌地图标记位置发送到拖动的烧瓶
- 谷歌地图地理编码和初始化顺序
- 如何绑定谷歌地图地理编码器.geocode() 回调函数
- 如何在谷歌地图地理位置API中获取笔记本电脑(wifi)的纬度和经度
- 谷歌地图地理位置刷新页面
- 谷歌地图地理位置getcurrentposition()