Por lo tanto, dependiendo del tamaño de sus geometrías, es posible que no desee obtener todo el polígono como parte de la solicitud inicial. (Los tiempos de carga se volverían terribles)
Así que crea una vista a la que puedas llegar con ajax:
def get_location_polygons(request):
response_data = {'data':None,}
location = get_object_or_404(County, cd=request.GET['code'])
# Build the polygons
response_data['data'] = {
'polygons' : location.geom.geojson,
'title' : location.cd
}
return HttpResponse(json.dumps(response_data), mimetype="application/json")
Luego llama a algún JS para analizar/mostrar el polígono:
function fetch_polygons(code)
{
$.ajax({
url : '/get-location-polygons/', // Whatever URL You decide for that AJAX request
data : {'code': code},
dataType : 'json',
type : 'GET',
success: function(data)
{
poly = JSON.parse(data.data['polygons'])
var paths = coord_to_paths(poly.coordinates);
polygon = new google.maps.Polygon({
paths : paths,
strokeColor : "#CCC",
strokeOpacity : .5,
strokeWeight : 1,
fillColor : "#CCC",
fillOpacity : .5
});
polygon.setVisible(true)
polygon.setMap(map); // Your map reference
polygon.infoWindow = new google.maps.InfoWindow({
content: '<strong>' + data.data.title + '</strong>',
});
}
});
}
function coord_to_paths(coords)
{
var paths = [];
for (var i = 0; i < coords.length; i++)
{
for (var j = 0; j < coords[i].length; j++)
{
var path = [];
for (var k = 0; k < coords[i][j].length; k++)
{
path.push(ll);
}
paths.push(path);
}
}
return paths;
}
coord_to_paths es bastante importante para hacer que los objetos multipoligonales se comporten bien en la V2 de la api de gmaps.