javascript - Convert Well-known text (WKT) from MySQL to Google Maps polygons with PHP -


i have searched on net , havent found solution this...any solution found doesnt work me... have row varchars , other these geometry types, eg:

polygon((23.953261 37.733372,23.953623 37.733202,23.953572 37.733132,23.954075 37.732896,23.954126 37.732966,23.954550 37.732767,23.954566 37.732698,23.954467 37.732603,23.954258 37.732476,23.953007 37.733041,23.953207 37.733303,23.953261 37.733372),(23.953695 37.732771,23.954243 37.732524,23.954427 37.732635,23.954496 37.732702,23.954484 37.732757,23.954133 37.732921,23.954087 37.732859,23.953525 37.733122,23.953577 37.733192,23.953292 37.733326,23.953080 37.733050,23.953695 37.732771))

and

multipolygon(((23.949134 37.734540,23.948811 37.734215,23.948775 37.734238,23.949077 37.734541,23.948689 37.734820,23.948809 37.734747,23.949134 37.734540)),((23.948536 37.734531,23.948449 37.734447,23.948414 37.734470,23.948472 37.734526,23.948420 37.734560,23.948449 37.734588,23.948536 37.734531)))

and simple polygons without outer rings....

i want to: query mysql parse data, pass them js , draw them google maps. that?

sounds me comes down regular expression question don't know how correctly pull coordinate sets out of strings, correct? regular expressions friend here, googling them understand shall demonstrate here.

<!doctype html public "-//w3c//dtd html 4.01//en" "http://www.w3.org/tr/html4/strict.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>some title</title> </head> <body> <div id="map_canvas" style="width:800px; height:600px;"> </div> <?php  $polystring1 = 'polygon((23.953261 37.733372,23.953623 37.733202,23.953572 37.733132,23.954075 37.732896,23.954126 37.732966,23.954550 37.732767,23.954566 37.732698,23.954467 37.732603,23.954258 37.732476,23.953007 37.733041,23.953207 37.733303,23.953261 37.733372),(23.953695 37.732771,23.954243 37.732524,23.954427 37.732635,23.954496 37.732702,23.954484 37.732757,23.954133 37.732921,23.954087 37.732859,23.953525 37.733122,23.953577 37.733192,23.953292 37.733326,23.953080 37.733050,23.953695 37.732771))';  $polystring2 = 'multipolygon(((23.949134 37.734540,23.948811 37.734215,23.948775 37.734238,23.949077 37.734541,23.948689 37.734820,23.948809 37.734747,23.949134 37.734540)),((23.948536 37.734531,23.948449 37.734447,23.948414 37.734470,23.948472 37.734526,23.948420 37.734560,23.948449 37.734588,23.948536 37.734531)))';  echo '<script type="text/javascript">'; //note quote styles below, important! echo "var polys=['$polystring1','$polystring2'];"; echo '</script>';  ?> <script src="https://maps.googleapis.com/maps/api/js?v=3&sensor=false"></script> <script type="text/javascript">  function parsepolystrings(ps) {     var i, j, lat, lng, tmp, tmparr,         arr = [],         //match '(' , ')' plus contents between them contain other '(' or ')'         m = ps.match(/\([^\(\)]+\)/g);     if (m !== null) {         (i = 0; < m.length; i++) {             //match numeric strings             tmp = m[i].match(/-?\d+\.?\d*/g);             if (tmp !== null) {                 //convert coordinate sets in tmp strings numbers , convert latlng objects                 (j = 0, tmparr = []; j < tmp.length; j+=2) {                     lat = number(tmp[j]);                     lng = number(tmp[j + 1]);                     tmparr.push(new google.maps.latlng(lat, lng));                 }                 arr.push(tmparr);             }         }     }     //array of arrays of latlng objects, or empty array     return arr; }  function init() {     var i, tmp,         myoptions = {             zoom: 16,             center: new google.maps.latlng(23.9511, 37.7337),             maptypeid: google.maps.maptypeid.roadmap         },         map = new google.maps.map(document.getelementbyid("map_canvas"), myoptions);     (i = 0; < polys.length; i++) {         tmp = parsepolystrings(polys[i]);         if (tmp.length) {             polys[i] = new google.maps.polygon({                 paths : tmp,                 strokecolor : '#ff0000',                 strokeopacity : 0.8,                 strokeweight : 2,                 fillcolor : '#ff0000',                 fillopacity : 0.35             });             polys[i].setmap(map);         }     } }  init();  </script> </body> </html> 

Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -