繁体   English   中英

JS控制台变量输出问题

[英]JS Console Variable Output Issue

我在运行一些我制作的JavaScript(从现有的Google python代码转换而来)以基于Google地图中矩形的纬度和经度值定义缩放级别时遇到问题。 我目前在几个变量的输出上有问题。.我已使用console.log()命令附加了一个图像,显示了有问题的变量及其输出。

代码错误

如您所见, bottom_lefttop_right变量分别与它们的分配bounds[0]bounds[1] 我确定我在此函数中做错了,因为输出与我将变量分配为的含义不同。 我还想知道为什么在console.log(bottom_left)使用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])
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^

我想你想让它成为2D阵列

CalculateBoundsZoomLevel([[southWestLat,southWestLng],[northEastLat,northEastLng]])

您将函数定义为:

function CalculateBoundsZoomLevel(bounds) {

因此在函数内部,变量bounds是调用的第一个参数

CalculateBoundsZoomLevel([southWestLat,southWestLng],[northEastLat,northEastLng])

因此bounds[0] == southWestLatbounds[1] == southWestLng 这与您在控制台中使用console.log(bounds[0])时不同。 然后,它使用以下定义的全局变量:

var bounds = new Array ();
bounds[0] = new Array (southWestLat,southWestLng);
bounds[1] = new Array (northEastLat,northEastLng)

全局bounds数组是二维数组,但是在函数内部,它只是一维数组。

您应该使用:

CalculateBoundsZoomLevel(bounds)

在函数内部使用与外部相同的数组。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM