views:

65

answers:

3

Hi folks,

I have some MultiPolygon data (The USA state of California) which I'm trying to display on a Google Map (v3 Javascript API).

The link to the data is here on CodePaste.NET. (Note: I pasted it there because the data would spam this post).

So I've tried to make it into some Json data .. and then display that MultiPolygon on a google map. It hangs/doesn't work :(

Here's the jQuery I've used...

function getTestData() {
    // Grab the California state data.
    var request = $.getJSON("/Foo/Bar?format=json", function (results) {
            // map it.
            var polygon = createGeoJsonPolygon(results, "#FF7800", "#46461F");
            polygon.setMap(map);
    });
}

function createGeoJsonPolygon(geojson, strokeColour, fillColour) {
    var coords = geojson.coordinates; // Array of polygons.
    var paths = [];

    $.each(coords, function (i, n) {
        $.each(n, function (j, o) {
            var path = [];
            $.each(o, function (k, p) {
                var ll = new google.maps.LatLng(p[1], p[0]);
                path.push(ll);
            });
            paths.push(path);
        });
    });

    return new google.maps.Polygon({
        paths: paths,
        strokeColor: strokeColour,
        strokeOpacity: 1,
        strokeWeight: 2,
        fillColor: fillColour,
        fillOpacity: 0.25
    });
}

Can anyone help? Anyone have a sec to give this a go and see if they can draw my sample data on a google map? I'm not sure if my code is wrong or the data as been extracted incorrectly.

A: 

You made the polygon; you didn't add it to any map.

Assuming map is a variable of type google.maps.Map, do this:

var poly = createGeoJsonPolygon( arg1, arg2, arg3 );
poly.setMap( map );

Also, note that your data (which you link to) is not (as you state) an array of points; it's an array of arrays of arrays of points.

tpdi
Ah crap. i left that code out .. (it was above the function). I'll update my post, above.
Pure.Krome
@tpdi : also, i have an array of polygons. and each polygon has an array of points. Is this wrong/bad thing?
Pure.Krome
No, the polygon.path should be an array of arrays of points. (But if it's just ansd array of points, the Google API will convert it for you as a convenience.)
tpdi
@tpdi :) kewl! so i'm trying to do the right thing. hmm. Did u have a try and see if my code hung your browser or displayed the state of california?
Pure.Krome
A: 

Without encoding polygon contours, you can use only one contour per polygon, see docs: http://code.google.com/intl/ru/apis/maps/documentation/javascript/v2/reference.html#GPolygon. It accepts only an array of points, not contours.

Read about polygon encoding for google maps, there is a readymade js for that.

edit: docs for v3: http://code.google.com/intl/ru/apis/maps/documentation/javascript/reference.html#PolygonOptions

alxx
@alxx hi mate - sorry, but I'm not sure what you mean :( *Encoded polygon contours*? I have an array of polygons. I based my code off this javascript: http://demos.geojason.info/complex-geojson-polygons-google-maps-demo.php (and that i think works).Can you also please explain this *polygon encoding* concept, please.
Pure.Krome
He's using v3, not v2.
tpdi
Sorry, I had solved similar problem recently in v2 and got fixed on that. As they say, if you have a hammer, all problems are looking like nails...In v3, there is no need to encoding, forget it. You can specify contours for one polygon explicitly (an array of arrays of points). If you have an array of polygons, you have a choice: create one complex polygon from those contours (each being an array of points), or create multiple polygon objects.
alxx
@alxx : ah ok. but isn't my code already doing that? if so .. any chance you could look at my code/data under a microscope to see what i've done wrong?
Pure.Krome
As tpdi has pointed out, your data seems to be an array of arrays of arrays of points, whereas you need an array of array of points. You need to "flatten" it down one level.Edit: no, it seems you already flatten it... Try to render some simple shape with 3 or 4 points.
alxx
@alxx - so you didn't try using my data set? are you also suggesting that the code I have is more or less correct... so it should be the data i've trying to display, is corrupted?
Pure.Krome
I'm only using v2 api for flash. Have you tried to create simple large polygon to be sure you can render anything?
alxx
A: 

My post on Google Groups came to help save the day.

This is what was suggested :

function createGeoJsonPolygon(geojson, strokeColour, fillColour) {
    var coords = geojson.coordinates; // Array of polygons.
    var paths = [];

    $.each(coords, function (i, n) { 
        var path = []; 
        $.each(n, function (j, p) { 
            var ll = new google.maps.LatLng(p[1], p[0]); 
            path.push(ll); 
        }); 
        paths.push(path); 
    }); 

    return new google.maps.Polygon({ 
        paths: paths, 
        strokeColor: "#FF0000", 
        strokeOpacity: 0.8, 
        strokeWeight: 1, 
        fillColor: "#FF0000", 
        fillOpacity: 0.35, 
        map: map 
    }); 
} 
Pure.Krome