Last active
January 1, 2016 03:18
-
-
Save nigelhanlon/8084296 to your computer and use it in GitHub Desktop.
Example JavaScript from the Working with GeoJSON tutorial at http://www.mapwolf.net/geojson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function addGeoJSON(geojson){ | |
// Create a GeoJSON parser that will convert our features | |
// to something OpenLayers understands. | |
var geojson_format = new OpenLayers.Format.GeoJSON({ | |
'internalProjection': new OpenLayers.Projection("EPSG:900913"), | |
'externalProjection': new OpenLayers.Projection("EPSG:4326") | |
}); | |
// Create a vector layer that our features will sit on. | |
var vector_layer = new OpenLayers.Layer.Vector("Mountains"); | |
// Add it to our map so we can see it. | |
map.addLayer(vector_layer); | |
// Convert our GeoJSON to OpenLayers features. | |
var openlayers_features = geojson_format.read(geojson); | |
// Finally, put our features on the map by adding them to | |
// our vector layer. | |
vector_layer.addFeatures(openlayers_features); | |
} | |
function init(){ | |
// Create a basic map using OpenStreetMap as the base layer. | |
map = new OpenLayers.Map( 'map' ); | |
openstreetmap_layer = new OpenLayers.Layer.OSM( "Simple OSM Map"); | |
map.addLayer(openstreetmap_layer); | |
// Lets centre the map around our point of interest. | |
// We also set the zoom level. | |
map.setCenter( | |
new OpenLayers.LonLat(-9.7427, 51.9994).transform( | |
new OpenLayers.Projection("EPSG:4326"), | |
map.getProjectionObject() | |
), 15 | |
); | |
// Add our GeoJSON to the Map | |
addGeoJSON(my_geojson); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment