[英]Polygon not show in Google Maps
I obtain the polygon coordinates through an AJAX request and I pass all the coordinates to an associative array. 我通过AJAX请求获得多边形坐标,然后将所有坐标传递给关联数组。
The problem is, when I create the Polygon this don't show, but if I create it with the coordinates example this show. 问题是,当我创建多边形时,它不会显示,但是如果我使用坐标示例创建它,则会显示此内容。
This is the code: 这是代码:
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.7281512, lng: -58.262616},
zoom: 10
});
var coords = new Array();
$.ajax({
type: 'GET',
url: 'http://nominatim.openstreetmap.org/reverse?format=json&osm_type=R&osm_id=2532299&polygon_geojson=1',
data: { get_param: 'value' },
success: function (data) {
$.each(data.geojson.coordinates[0], function( index, value ) {
if(typeof value[0] !== 'undefined' && typeof value[1] !== 'undefined') {
coords.push({lat: value[0], lng: value[1]});
}
});
}
});
var zone = new google.maps.Polygon({
paths: coords,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35
});
zone.setMap(map);
}
If I make the same, with the google example, this works fine. 如果我做同样的事情,用谷歌的例子,这很好。 I don't know why my associative array don't work.
我不知道为什么我的关联数组不起作用。
Code of the example: 示例代码:
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.7281512, lng: -58.262616},
zoom: 10
});
var triangleCoords = [
{lat: -34.817177, lng: -58.500948},
{lat: -34.688414, lng: -58.500948},
{lat: -34.688414, lng: -58.336764},
{lat: -34.817177, lng: -58.336764},
{lat: -34.817177, lng: -58.500948}
];
var zone = new google.maps.Polygon({
paths: triangleCoords,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35
});
zone.setMap(map);
}
You have two issues with the posted code. 您发布的代码有两个问题。
Your $.ajax
call is asynchronous, so the data isn't available when you try to populate the paths
attribute of the polygon. 您的
$.ajax
调用是异步的,因此当您尝试填充多边形的paths
属性时,数据不可用。 You need to use the data in the callback function when/where it is available. 您需要在可用的位置/位置使用回调函数中的数据。
You have the latitude and longitude reversed in the path of the polygon. 您在多边形的路径中颠倒了纬度和经度。
$.ajax({
type: 'GET',
url: 'http://nominatim.openstreetmap.org/reverse?format=json&osm_type=R&osm_id=2532299&polygon_geojson=1',
data: {
get_param: 'value'
},
success: function(data) {
var zone = new google.maps.Polygon({
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35,
map: map
});
$.each(data.geojson.coordinates[0], function(index, value) {
if (typeof value[0] !== 'undefined' && typeof value[1] !== 'undefined') {
coords.push({
lat: value[1],
lng: value[0]
});
}
zone.setPath(coords);
});
}
})
code snippet: 代码段:
var map; function initMap() { map = new google.maps.Map(document.getElementById('map'), { center: { lat: -34.7281512, lng: -58.262616 }, zoom: 10 }); var coords = new Array(); $.ajax({ type: 'GET', url: 'https://nominatim.openstreetmap.org/reverse?format=json&osm_type=R&osm_id=2532299&polygon_geojson=1', data: { get_param: 'value' }, success: function(data) { var zone = new google.maps.Polygon({ strokeColor: '#FF0000', strokeOpacity: 0.8, strokeWeight: 2, fillColor: '#FF0000', fillOpacity: 0.35, map: map }); $.each(data.geojson.coordinates[0], function(index, value) { if (typeof value[0] !== 'undefined' && typeof value[1] !== 'undefined') { coords.push({ lat: value[1], lng: value[0] }); } zone.setPath(coords); }); } }) } google.maps.event.addDomListener(window, "load", initMap);
html, body, #map { height: 100%; width: 100%; margin: 0px; padding: 0px }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maps.googleapis.com/maps/api/js"></script> <div id="map"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.