Guest User

cfengineers - GoogleMapsAPI Polygon from encoded V3

a guest
Sep 14th, 2010
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!--- this is the original function --->
  2. function decodeLine (encoded) {
  3.   var len = encoded.length;
  4.   var index = 0;
  5.   var array = [];
  6.   var lat = 0;
  7.   var lng = 0;
  8.  
  9.   while (index < len) {
  10.     var b;
  11.     var shift = 0;
  12.     var result = 0;
  13.     do {
  14.       b = encoded.charCodeAt(index++) - 63;
  15.       result |= (b & 0x1f) << shift;
  16.       shift += 5;
  17.     } while (b >= 0x20);
  18.     var dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
  19.     lat += dlat;
  20.  
  21.     shift = 0;
  22.     result = 0;
  23.     do {
  24.       b = encoded.charCodeAt(index++) - 63;
  25.       result |= (b & 0x1f) << shift;
  26.       shift += 5;
  27.     } while (b >= 0x20);
  28.     var dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
  29.     lng += dlng;
  30.  
  31.     array.push([lat * 1e-5, lng * 1e-5]);
  32.   }
  33.  
  34.   return array;
  35.  
  36.  
  37.  
  38.  
  39. <!---  this is what i am trying --->
  40. function decodeLine(encoded) {
  41.   var len = encoded.length;
  42.   var index = 0;
  43.   var array = [];
  44.   var lat = 0;
  45.   var lng = 0;
  46.  
  47.   while (index < len) {
  48.     var b;
  49.     var shift = 0;
  50.     var result = 0;
  51.     do {
  52.       b = encoded.charCodeAt(index++) - 63;
  53.       result |= (b & 0x1f) << shift;
  54.       shift += 5;
  55.     } while (b >= 0x20);
  56.     var dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
  57.     lat += dlat;
  58.  
  59.     shift = 0;
  60.     result = 0;
  61.     do {
  62.       b = encoded.charCodeAt(index++) - 63;
  63.       result |= (b & 0x1f) << shift;
  64.       shift += 5;
  65.     } while (b >= 0x20);
  66.     var dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
  67.     lng += dlng;
  68.  
  69.     array.push([new google.maps.LatLng(lat * 1e-5, lng * 1e-5)]);
  70.   }
  71.  
  72.   return array;
  73. }
  74.  
  75.  
  76.  
  77.  
  78. <!--- this is how i trying to use it --->
  79. var polygon_#fips#Coords = [];
  80.     var polygon_#fips#Coords = [decodeLine('#points#')];
  81.     var polygon_#fips#;
  82.    
  83.  
  84.     polygon_#fips# = new google.maps.Polygon({
  85.       paths: polygon_#fips#Coords,
  86.       strokeColor: "##FF0000",
  87.       strokeOpacity: 0.8,
  88.       strokeWeight: 3,
  89.       fillColor: "###polyfillcolor#",
  90.       fillOpacity: 0.35
  91.     });
  92.  
  93.     polygon_#fips#.setMap(map);
  94.  
  95. <!--- this is the orinigal use --->
  96. var polygon_#fips#Coords = [];
  97.     var polygon_#fips#Coords = [
  98.             new google.maps.LatLng(39.112456,-84.574779),
  99.             new google.maps.LatLng(39.314153,-84.261379),
  100.             new google.maps.LatLng(39.197099,-84.667579),
  101.             new google.maps.LatLng(39.16836,-84.479381)
  102.     ];
  103.    
  104.     var polygon_#fips#;
  105.    
  106.  
  107.     polygon_#fips# = new google.maps.Polygon({
  108.       paths: polygon_#fips#Coords,
  109.       strokeColor: "##FF0000",
  110.       strokeOpacity: 0.8,
  111.       strokeWeight: 3,
  112.       fillColor: "###polyfillcolor#",
  113.       fillOpacity: 0.35
  114.     });
  115.  
  116.     polygon_#fips#.setMap(map);
Add Comment
Please, Sign In to add comment