Thursday, 15 September 2011

javascript - Get geoJSON data from mapbox API to determine point in polygon -


I have recently followed the tutorial to determine whether a point shown by a marker is defined by the polygon Sits inside:

Works great, but they drag into polygon geographic data with AGX and apply it on the map. I have a mapbox map that is made from a polygon area already prepared. I am using my API to display my map. Now it is necessary to use geographic data to determine whether a point is within an area or not. How can I access that data via API?

Here's how I drag the map, GOJSON and a function to test if a point is in a ground area:

  // mapbox connection Var map = "& lt; map-id & gt;"; L.mapbox.accessToken = "& lt; my-token & gt;"; // init map var map = L.mapbox.map ("mapData", mapID, {attributionControl: false, zoomControl: false}) setView ([53.799, -1.548], 13, {PAN: {animated}}, zoom : {Living: true}}); // GeoJSON var geoJson; $ Http.get ("https://a.tiles.mapbox.com/v4/" + mapID + "/features.json?access_token=" + l.mapbox.accessToken) .vivet (data) {geoJson = Information; }); // Set the fixed point in the polygon function PointInPolygon () {var conno = [-1.3, 5.2]; Var Layer = LeafletPip.pointInLayer (Cores, L. Geogen (GOJSON), True); If (! Layer.length) // dot polygon does not have / / dot in polygon  

Integrated features are loaded in the L.mapbox.featureLayer which is available as a member of your L.mapbox.Map example. Suppose you have an example of your map stored in a reference named map map.featureLayer :

  // sure Please make sure that the feature layer is ready. Map.featureLayer.on ('ready', function (e) {// Loop on features e.target.eachLayer (function (layer) {// Your work / here is an example of "Layer" Marker / Polygon / Polyline // "Layer. Feature" assumes actual geographic feature});});  

An example of this concept is:

If you want you can also load different tiles and attributes, do not code your map with the code mapid :

  var map = l.mapbox.map ('mapbox', empty, {'center': [0, 0], 'zoom': 1}); Var Tile Layer = L. Mapbox. Tile layer ('yourMapId') AddTo (map); Var featureLayer = L. Mapbox. Feature Layer ('yourMapId') AddTo (map); Example:  

You can then leave the addTo method, first process your features and then add the layer to the map:

  var FeatureLayer = L. Mapbox. FeatureLayer ('yourMapId'); // Make sure that the feature layer is ready feature layer.Or ('ready', function (e) {// features at the eupport eeTarget.eachLayer (function (layer) {// your point}}; // layer E.target.addTo (map) for the map;});  

Whatever works best for you but I really forgot to answer your question (though I think you will not need it), the actual geographic feature is selected by the L.mapbox.FeatureLayer 's getGeoJSON method (only when ready for level):

  // Make sure the feature is ready on feature lair ('ready', Function (e) {// bring you featurecrollen variant = e.target.getGeoJSON ();});  

No comments:

Post a Comment