【问题标题】:GoogleMaps API v3 polygons from encoded, array problem来自编码数组问题的 GoogleMaps API v3 多边形
【发布时间】:2010-09-14 18:47:21
【问题描述】:

我可以解码点,我只需要弄清楚如何遍历点数组并生成

[
new google.maps.LatLng(39.112456,-84.574779),
new google.maps.LatLng(39.314153,-84.261379),
new google.maps.LatLng(39.197099,-84.667579),
new google.maps.LatLng(39.16836,-84.479381)
];

可在http://pastebin.com/Zf6hi4AB获取代码

感谢任何帮助。

<!--- this is the original function --->
function decodeLine (encoded) {
  var len = encoded.length;
  var index = 0;
  var array = [];
  var lat = 0;
  var lng = 0;

  while (index < len) {
    var b;
    var shift = 0;
    var result = 0;
    do {
      b = encoded.charCodeAt(index++) - 63;
      result |= (b & 0x1f) << shift;
      shift += 5;
    } while (b >= 0x20);
    var dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
    lat += dlat;

    shift = 0;
    result = 0;
    do {
      b = encoded.charCodeAt(index++) - 63;
      result |= (b & 0x1f) << shift;
      shift += 5;
    } while (b >= 0x20);
    var dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
    lng += dlng;

    array.push([lat * 1e-5, lng * 1e-5]);
  }

  return array;




<!---  this is what i am trying --->
function decodeLine(encoded) {
  var len = encoded.length;
  var index = 0;
  var array = [];
  var lat = 0;
  var lng = 0;

  while (index < len) {
    var b;
    var shift = 0;
    var result = 0;
    do {
      b = encoded.charCodeAt(index++) - 63;
      result |= (b & 0x1f) << shift;
      shift += 5;
    } while (b >= 0x20);
    var dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
    lat += dlat;

    shift = 0;
    result = 0;
    do {
      b = encoded.charCodeAt(index++) - 63;
      result |= (b & 0x1f) << shift;
      shift += 5;
    } while (b >= 0x20);
    var dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
    lng += dlng;

    array.push([new google.maps.LatLng(lat * 1e-5, lng * 1e-5)]);
  }

  return array;
}




<!--- this is how i trying to use it --->
var polygon_#fips#Coords = [];
    var polygon_#fips#Coords = [decodeLine('#points#')];
    var polygon_#fips#;


    polygon_#fips# = new google.maps.Polygon({
      paths: polygon_#fips#Coords,
      strokeColor: "##FF0000",
      strokeOpacity: 0.8,
      strokeWeight: 3,
      fillColor: "###polyfillcolor#",
      fillOpacity: 0.35
    });

    polygon_#fips#.setMap(map);

<!--- this is the orinigal use --->
var polygon_#fips#Coords = [];
    var polygon_#fips#Coords = [
            new google.maps.LatLng(39.112456,-84.574779),
            new google.maps.LatLng(39.314153,-84.261379),
            new google.maps.LatLng(39.197099,-84.667579),
            new google.maps.LatLng(39.16836,-84.479381)
    ];

    var polygon_#fips#;


    polygon_#fips# = new google.maps.Polygon({
      paths: polygon_#fips#Coords,
      strokeColor: "##FF0000",
      strokeOpacity: 0.8,
      strokeWeight: 3,
      fillColor: "###polyfillcolor#",
      fillOpacity: 0.35
    });

    polygon_#fips#.setMap(map);

【问题讨论】:

  • 你的一堆代码没有被格式化为代码。
  • 您已经向我们展示了您的代码以及您希望它做什么。你正在尝试array.push([new google.maps.LatLng(lat * 1e-5, lng * 1e-5)]); 如果你说明你当前的尝试是如何失败的,那将会很有帮助。例如。有错误吗?或者数组出来是空的?还是...?
  • 这就是我想要制作的 [新 google.maps.LatLng(39.112456,-84.574779), 新 google.maps.LatLng(39.314153,-84.261379), 新 google.maps.LatLng( 39.197099,-84.667579), 新的 google.maps.LatLng(39.16836,-84.479381)];在 polygon_#fips# = new google.maps.Polygon({ 路径:polygon_#fips#Coords, strokeColor: "##FF0000", strokeOpacity: 0.8, strokeWeight: 3, fillColor: "###polyfillcolor#",填充不透明度:0.35 });谢谢

标签: javascript arrays google-maps-api-3


【解决方案1】:

好的,我想我明白你在说什么了。尝试改变

 var polygon_#fips#Coords = [decodeLine('#points#')];

 var polygon_#fips#Coords = decodeLine('#points#');

也在 decodeLine() 改变

 array.push([new google.maps.LatLng(lat * 1e-5, lng * 1e-5)]);

 array.push(new google.maps.LatLng(lat * 1e-5, lng * 1e-5));

您所做的是在数组末尾添加一个新的 google.maps.LatLng 数组,因此您最终得到了一个 google.maps.LatLng 数组。 通过此更改,您最终应该会得到一个 google.maps.LatLng 数组,这正是您所需要的。

【讨论】:

  • firebug 中没有报告错误,只是地图上没有多边形,我已经在 v2 中测试了编码点,它工作正常。在此处完成 ColdFusion 代码 pastebin.com/FE08TzFN 感谢您的帮助。
  • 原始函数只是在数组中产生点(39.314153,-84.261379),但需要在数组中输出新的 google.maps.LatLng(39.314153,-84.261379)不知道该怎么做,我认为可能使用原始函数并以某种方式通过返回的数组重申以创建一个包含新 google.maps.LatLng(points[0],points[1]) 的新数组,这就是我被卡住的地方。跨度>
  • LarsH,这完全成功了!我已经为此出汗了一段时间。把我指向你的愿望清单。谢谢,厄尼
  • @cfEngineers,你是第一个向我询问愿望清单的人。如果您愿意,可以向samaritanspurse.org 捐款。很高兴能提供帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-30
  • 1970-01-01
  • 1970-01-01
  • 2014-07-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多