gpt4 book ai didi

javascript - 计算Google map 中其他用户视口(viewport)的相对位置

转载 作者:行者123 更新时间:2023-12-03 12:14:18 31 4
gpt4 key购买 nike

我正在实现 Google map 协作器,其中其他用户的 View 端口表示为矩形。如果任何用户 View 端口发生变化,矩形位置就会更新。

现在我想实现如果用户视口(viewport)不在我的视口(viewport)上,我需要创建一个代表用户当前视口(viewport)方向的头(黑色圆圈)。我目前拥有其他用户 View 端口的边界(LatLng)位置。为了绘制头部,我需要计算当前用户位置的 Angular 。

您可以引用下图来了解我想说的内容。如果用户的视口(viewport)在我的视口(viewport)内,我可以在矩形上画一个头。但是如果用户 View 端口发生变化,我需要计算相对于用户 View 端口绘制头部(黑色圆圈)的距离和 Angular 。
您能给我一些建议来完成它吗?如果您已经做过与此相关的事情,请分享您的想法。

enter image description here

最佳答案

谷歌地图 Bounds() .. 获取视口(viewport)顶部、左侧、右侧、按钮的位置:)

var latlgnBound = map.getBounds();
var NorthEast = latlgnBound.getNorthEast();
var SouthWest = latlgnBound.getSouthWest();
var maxlatX = NorthEast.lat();
var minlatX = SouthWest.lat();
var maxlngY = NorthEast.lng();
var minlngY = SouthWest.lng();

if (userlocation.locationx < maxlatX && userlocation.locationx > minlatX) {
if (userlocation.locationy < maxlngY && userlocation.locationy > minlngY) {
// draw a head :) .
}
}

抱歉,如果这不是您想要的;

外部用户;

**if (userlocation.locationx > maxlatX || userlocation.locationx < minlatX) 
{
// user is outside your view
}
if (userlocation.locationy > maxlngY || userlocation.locationy < minlngY) {
// user is outside your view
}
}**

更新

var angle = 0;
if(userlocation.locationx > maxlatX) {
angle += 1;
}
if(userlocation.locationy > maxlngY){
angle += 3;
}
if(userlocation.locationx < minlatX){
angle += 5;
}
if(userlocation.locationy < minlngY){
angle += 7;
}
switch(angle){
case 1:
//User in top view port
break;
case 3:
//User in left view port
break;
case 5:
//User in bottom view port
break;
case 7:
//User in right view port
break;
case 4:
//User in top right view port
break;
case 6:
//User in top left view port
break;
case 10:
//User in right bottom view port
break;
case 12:
//User in left bottm view port
break;
};

使用 HTML5 获取用户位置:)

  // Try HTML5 geolocation
if(navigator.geolocation) { //Nếu trình duyệt định vị được
navigator.geolocation.getCurrentPosition(function(position) {
//Lấy vị trí người dùng
//Get location of user
userlocation.locationx = position.coords.latitude;
userlocation.locationy = position.coords.longitude

}, function() {
handleNoGeolocation(true); //Không xác định được vị trí
});
} else {
// Browser doesn't support Geolocation
handleNoGeolocation(false);
}
function handleNoGeolocation(errorFlag){
if (errorFlag) {
var content = "Can't get user location";
} else {
var content = "Browser doesn't support get location";
}
}

或阅读更多:http://www.w3schools.com/html/html5_geolocation.asp

关于javascript - 计算Google map 中其他用户视口(viewport)的相对位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24800324/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com