Google Maps API v3多边形来自编码,数组问题

时间:2010-09-14 18:47:21

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

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

[
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);

1 个答案:

答案 0 :(得分: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数组,这是你需要的。