English 中文(简体)
在 Google 地图 v2 上绘制多边形
原标题:draw polygons on google map v2

我如何能够在 Google 地图上用用户输入坐标绘制一个多边形。 我有两个输入字段( lat and lng), 当用户单击我要绘制多边形的按钮时。 我有这个代码, 但是它不起作用 。 有人能帮我吗?

HTML :

<form onsubmit="show(this.lat.value, this.lon.value); return false;">
    <p><label>Latituda</label></p>
       <input type="text" size="13" id="latbox2" name="lat" value="">
    <p><label>Longituda</label></p>
       <input type="text" size="13" id="lonbox2" name="lon" value="">                        
    <p><input type="submit" id="button2" value="Show" class="btnsearch" ></p>
</form>

JavaScript :

function xz() {
    if (GBrowserIsCompatible()){
            map = new GMap2(document.getElementById("map_canvas"));
            map.setCenter(new GLatLng(43.500752, 16.435547), 6);
            map.setUIToDefault();
            map.addMapType(G_SATELLITE_3D_MAP);
            map.enableGoogleBar();
    }
  }
    function show(lat, lon){
        var latOffset = 0.01;
        var lonOffset = 0.01;
        var polygon = new GPolygon([
            new GLatLng(lat, lon - lonOffset),
            new GLatLng(lat + latOffset, lon),
            new GLatLng(lat, lon + lonOffset),
            new GLatLng(lat - latOffset, lon),
            new GLatLng(lat, lon - lonOffset)
        ], "#f33f00", 5, 1, "#ff0000", 0.2);
        map.addOverlay(polygon);
    }
最佳回答

您实际上不说这有什么问题, 但实验过它后, 似乎GLatLng () 需要数字而不是字符串。 文档中确实说要使用数字; 输入字段的内容是字符串 。

将它们转换为数字似乎有效( 虽然缩放 6 表示多边形非常小 ) 。 请注意, 我在结尾处也添加了一个 < code> setCenter () , 这样它就可以看到 。

var lat = parseFloat(lat);
var lon = parseFloat(lon);

http://jsfiddle.net/csRCd/1/

问题回答

暂无回答




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签