使用PHP将众所周知的文本(WKT)从MySQL转换为Google Maps多边形 [英] Convert Well-known text (WKT) from MySQL to Google Maps polygons with PHP

查看:134
本文介绍了使用PHP将众所周知的文本(WKT)从MySQL转换为Google Maps多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜遍了整个网络,我还没有找到任何解决方案...我发现任何解决方案都不适合我...我有一行VARCHARS和其他与这些GEOMETRY类型,例如:



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 (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))
$ (((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)),((b)(b)和(b) (23.948536 37.734531,23.948449 37.734447,23.948414 37.734470,23.948472 37.734526,23.948420 37.7345 60,23.948449 37.734588,23.948536 37.734531)))

和没有外圈的简单多边形....

我想:
查询mysql
解析数据,
将它们传递给js
并将它们绘制到Google Maps。
对此有何帮助?

解决方案

听起来像这样,真正归结为一个正则表达式问题,不知道如何正确地从这些字符串中拉出所有的坐标集,对吗?正则表达式是你的朋友在这里,做一些关于他们的理解,因为我只能在这里演示。

 <!DOCTYPE html PUBLIC -  // W3C // DTD HTML 4.01 // ENhttp:// www .w3.org / TR / HTML4 / strict.dtd> 
< html>
< head>
< meta http-equiv =content-typecontent =text / html; charset = utf-8>
< title> Some Title< / title>
< / head>
< body>
< div id =map_canvasstyle =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))'; (((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>';
//注意下面的引用样式,重要!
echovar polys = ['$ polyString1','$ polyString2'];;
echo'< / script>';

?>
< script src =https://maps.googleapis.com/maps/api/js?v=3&sensor=false>< / script>
< script type =text / javascript>

函数parsePolyStrings(ps){
var i,j,lat,lng,tmp,tmpArr,
arr = [],
// match'和''''加上它们之间的内容,它们包含除'('或')'之外的任何内容
m = ps.match(/ \([^ \(\)] + \)/ g);
if(m!== null){
for(i = 0; i< m.length; i ++){
//匹配所有数字字符串
tmp = m [I] .match(/ - \d + \ \d * /克??);
if(tmp!== null){
//将tmp中的所有坐标集合从字符串转换为Numbers并转换为LatLng对象
for(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);



// LatLng对象数组或者空数组
return arr;


函数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);
for(i = 0; i< 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>


I have searched all over the net and i havent found any solution for this...Any solution I found doesnt work for me... I have a row with VARCHARS and an other with 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 to js and draw them to Google Maps. Any help with that?

解决方案

Sounds to me like this really comes down to a regular expression question as you simply don't know how to correctly pull all the coordinate sets out of those strings, correct? Regular expressions are your friend here, do some googling about them to understand as I shall only 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 the 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 '(' and ')' plus contents between them which contain anything other than '(' or ')'
        m = ps.match(/\([^\(\)]+\)/g);
    if (m !== null) {
        for (i = 0; i < m.length; i++) {
            //match all numeric strings
            tmp = m[i].match(/-?\d+\.?\d*/g);
            if (tmp !== null) {
                //convert all the coordinate sets in tmp from strings to Numbers and convert to LatLng objects
                for (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);
    for (i = 0; i < 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>

这篇关于使用PHP将众所周知的文本(WKT)从MySQL转换为Google Maps多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆