javascript - How to use a polygon string in google maps api -


i have following polygon string:

polygon ((5.031728766 52.016855117, 5.039437914 52.018712029, 5.038732065 52.01933205, 5.03880625 52.019536002, 5.036666299 52.021123062, 5.037225302 52.021436208, 5.036494826 52.021980534, 5.040069034 52.024180983, 5.041131857 52.023541011, 5.041485972 52.023745389, 5.042328698 52.023235595, 5.043167194 52.022781293, 5.043379189 52.022938683, 5.04366399 52.022788333, 5.044615961 52.023393034, 5.046878469 52.022023355, 5.047609948 52.02119413, 5.048777737 52.022018526, 5.049465821 52.022060318, 5.05135083 52.021274278999996, 5.053039915 52.020873436, 5.052288001 52.019935439, 5.052174884 52.019294199, 5.053026298 52.019318482, 5.053120663 52.018982405, 5.05237284 52.018935127, 5.051442801 52.019120203, 5.046607457 52.016128313, 5.046220739 52.015628312, 5.04412241 52.015134981, 5.043853082 52.015544473, 5.043410675 52.015932024, 5.042704158 52.016254485, 5.042235947 52.016357569, 5.040118936 52.0166409, 5.039579367 52.015163505, 5.034087326 52.015907152, 5.03224395 52.016039016, 5.031728766 52.016855117), (5.043324081 52.017406693, 5.046676295 52.019354241, 5.048003676 52.020235065, 5.046772806 52.021010583, 5.045897693 52.02180469, 5.043619067 52.020981305, 5.042189351 52.020258164, 5.039736347 52.018909018, 5.041350353 52.018037167, 5.042763839 52.01739758, 5.042763839 52.01739758, 5.043324081 52.017406693)) 

i use plot simple polygon using google maps api, e.g.:

var trianglecoords = [     new google.maps.latlng(25.774252, -80.190262),     new google.maps.latlng(18.466465, -66.118292),     new google.maps.latlng(32.321384, -64.75737),     new google.maps.latlng(25.774252, -80.190262)   ]; 

how can i:

  • either iterate on text , grab coordinates
  • use alternative more efficient way google.maps.latlng(polygontextstring)

using modified code this related question: polygon array not work in google map api. string formatted differently, don't know if on purpose.

proof of concept fiddle

code snippet:

var map;  var bounds = new google.maps.latlngbounds();    // polygon  var polygonstr = "polygon ((5.031728766 52.016855117, 5.039437914 52.018712029, 5.038732065 52.01933205, 5.03880625 52.019536002, 5.036666299 52.021123062, 5.037225302 52.021436208, 5.036494826 52.021980534, 5.040069034 52.024180983, 5.041131857 52.023541011, 5.041485972 52.023745389, 5.042328698 52.023235595, 5.043167194 52.022781293, 5.043379189 52.022938683, 5.04366399 52.022788333, 5.044615961 52.023393034, 5.046878469 52.022023355, 5.047609948 52.02119413, 5.048777737 52.022018526, 5.049465821 52.022060318, 5.05135083 52.021274278999996, 5.053039915 52.020873436, 5.052288001 52.019935439, 5.052174884 52.019294199, 5.053026298 52.019318482, 5.053120663 52.018982405, 5.05237284 52.018935127, 5.051442801 52.019120203, 5.046607457 52.016128313, 5.046220739 52.015628312, 5.04412241 52.015134981, 5.043853082 52.015544473, 5.043410675 52.015932024, 5.042704158 52.016254485, 5.042235947 52.016357569, 5.040118936 52.0166409, 5.039579367 52.015163505, 5.034087326 52.015907152, 5.03224395 52.016039016, 5.031728766 52.016855117), (5.043324081 52.017406693, 5.046676295 52.019354241, 5.048003676 52.020235065, 5.046772806 52.021010583, 5.045897693 52.02180469, 5.043619067 52.020981305, 5.042189351 52.020258164, 5.039736347 52.018909018, 5.041350353 52.018037167, 5.042763839 52.01739758, 5.042763839 52.01739758, 5.043324081 52.017406693))";      function initialize() {    map = new google.maps.map(      document.getelementbyid("map_canvas"), {        center: new google.maps.latlng(37.4419, -122.1419),        zoom: 13,        maptypeid: google.maps.maptypeid.roadmap      });    drawpoly(polygonstr);    map.fitbounds(bounds);    }    function drawpoly(multipolygonwkt) {    var polylines = [];    var toreturn = [];    multipolygonwkt = multipolygonwkt.replace("polygon ", "");    var formattedvalues = multipolygonwkt.replace("))", "");    formattedvalues = formattedvalues.replace("((", "");      var linescoords = formattedvalues.split("), (");      (i = 0; < linescoords.length; i++) {      polylines[i] = [];      var singleline = linescoords[i].split(", ");        (j = 0; j < singleline.length; j++) {        var coordinates = singleline[j].split(" ");        var latlng = new google.maps.latlng(parsefloat(coordinates[1]), parsefloat(coordinates[0]));        bounds.extend(latlng);          polylines[i].push(latlng);        }    }      toreturn.push(      new google.maps.polygon({        map: map,        paths: polylines,        strokecolor: 'red',        strokeopacity: 1,        strokeweight: 2,        zindex: 1      }));    return toreturn;  }    google.maps.event.adddomlistener(window, "load", initialize);
html,  body,  #map_canvas {    height: 500px;    width: 500px;    margin: 0px;    padding: 0px  }
<script src="https://maps.googleapis.com/maps/api/js"></script>    <div id="map_canvas" style="width:750px; height:450px; border: 2px solid #3872ac;"></div>


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -