0

I am trying to draw polygon in my map but fail. I have set latlng from my database. I already read the tutorial in here https://developers.google.com/maps/documentation/javascript/examples/polygon-simple

I am using vb.net on query.

here is my code :

function showpolygon(){
       var data1 = "<%= vpolygon %>";
       var ndata1 = data1.split("|");            
       var len1 = ndata1.length;
       var nnama, nlat, nlong, gmbrpolygon;
       var arraypoly = [];
       var arraypoly1 = [];
       var n =0;
       var polygons = [];
       alert ("<%= vpolygon %>");
       for (var i = 0; i < len1; i++)
            {
               ndata11 = ndata1[i].split("~");
               nlat = parseFloat (ndata11[1]);
               nlong = parseFloat(ndata11[2]);
               arraypoly = new google.maps.LatLng(nlat,nlong);  
                //alert(polygons);
               arraypoly1.push(arraypoly);          
               }

        polygons.push(arraypoly1);
        gmbrpolygon = new google.maps.Polygon({
        paths: arraypoly1,
        strokeColor: '#FF0000',
        strokeOpacity: 0.8,
        strokeWeight: 2,
        fillColor: '#FF0000',
        fillOpacity: 0.35
        });
        gmbrpolygon.setMap(map);
       }

I am trying to using a mvc array but an error occurs.

edit :<%= vpolygon %> contain : name~lat~long~numberof|

polygon[n] = { location: new google.maps.LatLng(nlat,nlong)};
var polygons = new google.maps.MVCArray(polygon);

the error when i am using mvc array is parameter 0: [object][object] and Cannot set property '0' of undefined

and with this code nothing error in console javascript, but the polygon doesn't appear.

4

1 回答 1

0

当提供的字符串具有正确的格式时,您的函数对我来说很好:

'some~lat~lng|some~lat~lng'

检查字符串是否有这种格式,我猜它确实有这种格式:

'lat~lng|lat~lng'

当它执行时,您的函数将不起作用,因为您从每个部分的拆分值 2+3 创建 LatLngs(请注意,数组索引是从零开始的),但结果数组将只有 2 个项目。

你必须改变:

       nlat = parseFloat (ndata11[1]);
       nlong = parseFloat(ndata11[2]);

进入

       nlat = parseFloat (ndata11[0]);
       nlong = parseFloat(ndata11[1]);

<edit>

由于数据的格式不是这里的问题(我仍然不清楚为什么没有 MVCArray 的第一种方法不起作用),我重写了你的函数,它现在使用 MVCArray,支持复杂的多边形并接受数据和多边形-选项作为参数以使此功能灵活:

function showpolygon(data,options){
       var indexes={lat:1,lng:2},//indexes of lat and lng in the splitted arrays
           delimiters   = ['#','|','~'],//delimiters for data
           paths        = new google.maps.MVCArray(),
           defaults     ={//default polygon options,
                          //will be merged with the options-argument
              strokeColor: '#FF0000',
              strokeOpacity: 0.8,
              strokeWeight: 2,
              fillColor: '#FF0000',
              fillOpacity: 0.35
            }, temp;

       if(options){
          for(var k in options){
            defaults[k]=options[k]
          }
       }

       defaults.paths=paths;
       temp=data.split(delimiters[0]); 

       for (var a = 0; a < temp.length; a++){
               var current=new google.maps.MVCArray();
               temp[a]=temp[a].split(delimiters[1]);
               for (var b = 0; b < temp[a].length; b++){
                 temp[a][b]=temp[a][b].split(delimiters[2]);
                 current.push(new google.maps.LatLng(temp[a][b][indexes.lat],
                                                     temp[a][b][indexes.lng]))
               }

               paths.push(current);          
            }
    return new google.maps.Polygon(defaults);
}

简单多边形的示例用法:

showpolygon("name~54~-82~0|name~55.3~-116~0|name~34~-107~0|name~36~-70~0",
            {map:map, strokeColor: '#000000', strokeWeight:4, fillColor:'#00FF00'});

复杂多边形的示例用法:(注意新的分隔符#,分隔符可以通过函数内部的 delimiters-variable 定义)

showpolygon("name~1~1~0|name~55.3~55~0|name~43.2~-11~0#name~24~4|name~39~1~0|name~43~22~0|name~31.5~20.8~0",
            {map:map, strokeColor: '#0000FF'});

演示:http: //jsfiddle.net/doktormolle/rBE6S/

于 2013-08-20T13:48:36.957 回答