JS控制台变量输出问题

时间:2019-07-02 18:10:04

标签: javascript html

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

Code error

您会看到bottom_lefttop_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)

一如既往,我们将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:0)

很简单,您要将两个参数传递给需要一个参数的函数。

function CalculateBoundsZoomLevel(bounds) {
                                  ^^^^^^

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

我假设您希望它是2D阵列

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

答案 1 :(得分:0)

您将函数定义为:

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数组是2维数组,但在函数内部只是1维数组。

您应该使用:

CalculateBoundsZoomLevel(bounds)

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