javascript - JS 控制台变量输出问题
问题描述
我在运行我制作的一些 javascript(从现有的 Google python 代码转换)以根据谷歌地图中矩形的纬度和经度值定义缩放级别时遇到问题。我目前遇到了几个变量的输出问题。我附上了一张图片,显示了有问题的变量及其使用console.log()
命令的输出。
如您所见,bottom_left
和top_right
变量分别不同于它们的赋值bounds[0]
和bounds[1]
。我确定我在此函数中做错了什么,因为输出与我将变量分配为的意思不同。我也想知道为什么在使用console.log(bottom_left)
或console.log(top_right)
在控制台中会出现问题?是因为这些变量不是全局定义的吗?
总体而言,代码无法正常运行,因为无论输入什么纬度/经度值,它都会输出最大缩放(理论上缩放级别应该随着纬度/经度范围的增加而变得越来越小)。
以下是示例中的完整代码:
//Define initial variables
var southWestLat = 10;
var southWestLng = -180;
var northEastLat = 60;
var northEastLng = -50;
var bounds = new Array ();
bounds[0] = new Array (southWestLat,southWestLng);
bounds[1] = new Array (northEastLat,northEastLng)
//------------------------------------
//------------------------------------
//------------------------------------
//Point constructor
function Point(x, y) {
this.x = x;
this.y = y;
}
//------------------------------------
//------------------------------------
//------------------------------------
function CalcWrapWidth(zoom) {
return pixel_range[zoom]
}
//------------------------------------
//------------------------------------
//------------------------------------
function range(lowEnd,highEnd){
var arr = [],
c = highEnd - lowEnd + 1;
while ( c-- ) {
arr[c] = highEnd--
}
return arr;
}
//------------------------------------
//------------------------------------
//------------------------------------
function Bound(value,opt_min,opt_max) {
if (opt_min != null) {
value = Math.max(value,opt_min);
}
if (opt_max != null) {
value = Math.min(value,opt_max);
}
return value;
}
//------------------------------------
//------------------------------------
//------------------------------------
//Converts from degrees to radians
function DegToRad(deg) {
return deg*(Math.pi/180);
}
//------------------------------------
//------------------------------------
//------------------------------------
//Gets center bounds, bounds as ['lat','lng']
function GetCenter(bounds) {
var center_lat = (bounds[1][0] + bounds[0][0])/2;
var center_lng = (bounds[0][1] + bounds[1][1])/2;
var center = new Array ();
center[0] = center_lat;
center[1] = center_lng;
return center;
}
//------------------------------------
//------------------------------------
//------------------------------------
//Prepare the calculation...
var pixels_per_lon_deg = new Array ();
var pixels_per_lon_rad = new Array ();
var pixel_origo = new Array ();
var pixel_range = new Array ();
var pixels = 640;
var zoom_levels = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18];
var pointObj = new Array ();
var origin;
function prime() {
for (i in zoom_levels) {
origin = pixels/2;
pixels_per_lon_deg.push(pixels/360);
pixels_per_lon_rad.push(pixels/(2*Math.pi));
pixel_origo.push({x:origin,y:origin});
pixel_range.push(pixels);
pixels = pixels*2;
}
}
//------------------------------------
//------------------------------------
//------------------------------------
//Convert from Lat Lng to pixel
function FromLatLngToPixel(lat_lng, zoom) {
o=pixel_origo[zoom];
x_cord=Math.round(o.x+lat_lng[1]*pixels_per_lon_deg[zoom]);
siny=Bound(Math.sin(DegToRad(lat_lng[0])),-0.9999,0.9999);
y_cord=Math.round(o.y+0.5*Math.log((1+siny) / (1-siny))*-pixels_per_lon_rad[zoom]);
pointObj = ({x:x_cord,y:y_cord}); //Potential error here?
return pointObj
}
//------------------------------------
//------------------------------------
//------------------------------------
/**Main function bounds: A list of length 2, each holding a list of length 2. It holds
the southwest and northeast lat/lng bounds of a map. It should look
like this: [[southwestLat, southwestLng], [northeastLat, northeastLng]]**/
function CalculateBoundsZoomLevel(bounds) {
var zmax=18;
var zmin=0;
var bottom_left=bounds[0];
var top_right=bounds[1];
var backwards_range=range(zmin,zmax).reverse();
var lng_dsc = Math.abs(bounds[0][1] - bounds[1][1]);
var lat_dsc = Math.abs(bounds[0][0] - bounds[1][0]);
var rrun = lat_dsc/lng_dsc;
var runr = lng_dsc/lat_dsc;
var vs_height;
var vs_length;
console.log(bottom_left) //Delete
console.log(top_right) //Delete
if (rrun<1) {
vs_height = 640*rrun;
vs_length = 640;
} else {
vs_height = 640;
vs_length = 640*runr;
}
var view_size = new Array (vs_length,vs_height);
for (z in backwards_range) {
var bottom_left_pixel=FromLatLngToPixel(bottom_left,z);
var top_right_pixel=FromLatLngToPixel(top_right,z);
if (bottom_left_pixel.x > top_right_pixel.x) {
bottom_left_pixel.x -= CalcWrapWidth(z);
}
if (Math.abs(top_right_pixel.x - bottom_left_pixel.x) <= view_size[0] && Math.abs(top_right_pixel.y - bottom_left_pixel.y) <= view_size[1]) {
return z
}
}
return 0
}
//------------------------------------
//------------------------------------
//------------------------------------
//Run function
prime()
CalculateBoundsZoomLevel([southWestLat,southWestLng],[northEastLat,northEastLng])
console.log(z)
一如既往地非常感谢任何帮助。谢谢!
解决方案
很简单,您将两个参数传递给需要一个参数的函数。
function CalculateBoundsZoomLevel(bounds) {
^^^^^^
CalculateBoundsZoomLevel([southWestLat,southWestLng],[northEastLat,northEastLng])
^^^^^^^^^^^^^^^^^^^^^^^^^^^
我假设你希望它是一个二维数组
CalculateBoundsZoomLevel([[southWestLat,southWestLng],[northEastLat,northEastLng]])
推荐阅读
- python - 创建一种编辑 .txt 文件的方法
- postgresql - 在 AWS Aurora/postgres 上收听/通知
- node.js - 尝试从 localhost:9000 到 3000 代理请求 /api/authe/signup 时发生错误 Econnrefused - Nuxt、Vue、VS 代码
- sql - 如何在 Postgres 中将数据从一个表移动到另一个表(使用 PK 时)
- r - 将数据表的结果从一个选项卡显示到另一个选项卡,并将其分配给 Shiny 中的数据集列表
- java - 我的应用程序:Android 10 屏幕录像机不再工作
- android - 水平显示 Spinner 项目
- rust - rust - 有没有办法改变并保持对闭包内变量的引用?
- python - 如何调整 PIL.ImageTk 图像的大小?
- bash - Bash 脚本 nohup 执行