6

I was under the impression that it was.

Here is my example:

<html lang='en'>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>GeoRSS</title>
<script src="http://maps.google.com/maps/api/js?v=3.5&amp;sensor=false"></script>
<script src="http://openlayers.org/api/OpenLayers.js"></script>
<script src="google-v3.js"></script>

<script type="text/javascript">

var map;

function init() {
    var restrictedExtent = new OpenLayers.Bounds(-180, -90, 180, 90);

    map = new OpenLayers.Map("map_element"
    );
    map.addControl(new OpenLayers.Control.MousePosition());
    map.addControl(new OpenLayers.Control.LayerSwitcher());

    var layer = new OpenLayers.Layer.Google("Google", {"sphericalMercator": true});

    var usgs = new OpenLayers.Layer.Vector("Earthquakes", {
        projection: new OpenLayers.Projection("EPSG:4326"),
        strategies: [new OpenLayers.Strategy.Fixed()],
        protocol: new OpenLayers.Protocol.HTTP({
            url: "http://earthquake.usgs.gov/earthquakes/catalogs/7day-M2.5.xml",
            format: new OpenLayers.Format.GeoRSS()
        })
    });
    map.addLayers([layer, usgs]);
    map.setCenter(new OpenLayers.LonLat(-11682133.90525, 4676768.13795), 4);
}

</script>

</head>
<body onload="init()">
    <div id="map_element">
    </div>
</body>
</html>

For me this is only working on IE. I would like to keep this xml file on the USGS server and call it dynamically rather than save it locally. Is there anyway to get this to work, on other browsers (besides IE) without using a proxy script?

Thanks!

2 Answers 2

7

JSONP is a way of getting around the same-origin policy by obtaining data via using "script" tags rather than trying to remotely extract information. In your case you can wrap the whole USGS response in jsonp using for example online service http://jsonpwrapper.com/.

Here is modified example:

<html lang='en'>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>GeoRSS</title>
<script src="http://maps.google.com/maps/api/js?v=3.5&amp;sensor=false"></script>
<script src="http://openlayers.org/api/OpenLayers.js"></script>
<script src="google-v3.js"></script>

<script type="text/javascript">

var map;

function init() {
    var restrictedExtent = new OpenLayers.Bounds(-180, -90, 180, 90);

    map = new OpenLayers.Map("map_element");
    map.addControl(new OpenLayers.Control.MousePosition());
    map.addControl(new OpenLayers.Control.LayerSwitcher());

    var layer = new OpenLayers.Layer.Google("Google", {"sphericalMercator": true});

    var usgs = new OpenLayers.Layer.Vector("Earthquakes", {
        projection: new OpenLayers.Projection("EPSG:4326"),
        strategies: [new OpenLayers.Strategy.Fixed()],
        protocol: new OpenLayers.Protocol.Script({
            url: "http://jsonpwrapper.com/?urls[]=http://earthquake.usgs.gov/earthquakes/catalogs/7day-M2.5.xml",
            format: new OpenLayers.Format.GeoRSS(),
            parseFeatures: function(data) {
                return this.format.read(data[0]['body'])
            }
        })
    });
    map.addLayers([layer, usgs]);
    map.setCenter(new OpenLayers.LonLat(-11682133.90525, 4676768.13795), 4);
}

</script>

</head>
<body onload="init()">
    <div id="map_element">
    </div>
</body>
</html>

Live demo.

2
  • jsonpwrapper looks to be just an outsourced proxy. you have no control over the stability of the proxy server and can't extend it's features [e.g. unzipping KMZ on the fly into KML]. Either way, the OP's solution is still to "get a proxy".
    – Vadim
    Commented Dec 13, 2011 at 18:34
  • @Vadim you are correct about the weaknesses of this solution. But Im glad that drnextgis posted it. I had never seen jsonwrapper before.
    – glw
    Commented Dec 14, 2011 at 7:31
3

You will have to setup a server-side proxy to retrieve vector data. Proxy documentation

You can search around online for sample proxy implementations. ESRI happens to have a few different languages implemented here including php, jsp and .net. These samples will be specific to ArcGIS, so you'll have to remove some restrictions; or just roll your own.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.