Supplement Mapbox geocoder results with external geocoder APISeeking geocoding with advanced language...

Equivalent of "illegal" for violating civil law

Why is it that Bernie Sanders is always called a "socialist"?

Is there a way to store 9th-level spells in a Glyph of Warding or similar method?

Concatenating two int[]

Why is that max-Q doesn't occur in transonic regime?

Plausible reason to leave the Solar System?

Why does 0.-5 evaluate to -5?

How can I have probability increase linearly with more dice?

Is there a verb that means to inject with poison?

Can you determine if focus is sharp without diopter adjustment if your sight is imperfect?

In harmony: key or the flow?

What's this assembly doing?

What is a good reason for every spaceship to carry a weapon on board?

How to politely refuse in-office gym instructor for steroids and protein

Stuck on a Geometry Puzzle

Critique vs nitpicking

How do I prevent a homebrew Grappling Hook feature from trivializing Tomb of Annihilation?

Not a Long-Winded Riddle

Has any human ever had the choice to leave Earth permanently?

How do you get out of your own psychology to write characters?

Count repetitions of an array

Does the ditching switch allow an A320 to float indefinitely?

How does Leonard in "Memento" remember reading and writing?

Taking headphones when quitting job



Supplement Mapbox geocoder results with external geocoder API


Seeking geocoding with advanced language support?Purely client-side geocoding options for a webmapLoad external tileJSON file in MapboxHow to Have Geocoding Service Return Results from Partial Street Name Input?Best approach for indexing a SQLite database containing geoJSON spatial objects?Mapbox GL JS Geocoder markerMapbox Error: Style is not done loadingMapBox geocoder results are not exact, how to enhance mapbox geocoding API?Geocoding to only reference Tileset using Mapbox GL JS?Looped query rendered feature function for a set of intersect points upon a GeoJSON building layer in Mapbox GL JS













0















I am still learning JS so please bear with me.



I have built a Mapbox GL JS map and would like to supplement Mapbox geocoding results with those from an external geocoding API as many POIs I need are not returned with the standard Mapbox geocoder (such as campsites and national parks).



I have followed the tutorial to supplement results by querying local data here: Supplement forward geocoding search results from another data source



I have got this to work with local a geojson source however I cant get it to work when querying an external geocoder. I am testing with OpenCage.



My code for the geocoder instance on the map is:



// Load geojson source data                
var myjson;
$.getJSON("<github hosted file>", function(json){
myjson = json;
});

// Local geocoder function
function forwardGeocoder(query) {

var matchingFeatures = [];
for (var i = 0; i < myjson.features.length; i++) {
var feature = myjson.features[i];
if (feature.properties.road_name.toLowerCase().search(query.toLowerCase()) !== -1) {
feature['place_name'] = '🚙 ' + feature.properties.road_name;
feature['center'] = feature.geometry.coordinates;
matchingFeatures.push(feature);
}
}

return matchingFeatures;
}

//Add geocoder/search box
var geocoder = new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
localGeocoder: forwardGeocoder,
country: 'au'
});


This works fine for local data pulled in from a geojson source (i.e. already loaded in the client side). When I try to add an external geocoder into the forwardGeocoder() function, the query returns the correct geocoded results and I can get them to appear in the console but they dont appear in the standard mapbox geocoder results list. Code for the external geocoder (using OpenCage to test).



function forwardGeocoder(query){
var matchingFeatures = [];
$.ajax({
url: 'https://api.opencagedata.com/geocode/v1/geojson',
method: 'GET',
data: {
'key': 'ab6ad6e3ad3043e2aeb897322ab038f0',
'q': query,
'pretty': 1
},
dataType: 'json',
statusCode: {
200: function(response){ // success
var listings = response.features;
//console.log(listings);
for (var i = 0; i < listings.length; i++) {
var feature = listings[i];
if (feature.properties.formatted.toLowerCase().search(query.toLowerCase()) !== -1) {
feature['place_name'] = '🚙 ' + feature.properties.formatted;
feature['center'] = feature.geometry.coordinates;
matchingFeatures.push(feature);
}
}
console.log(matchingFeatures);
return matchingFeatures;
},
402: function(){
console.log('hit free-trial daily limit');
console.log('become a customer: https://opencagedata.com/pricing');
}
}
});
}


Console output from this function:



0:
center: (2) [145.516244, -37.4666249]
geometry: {coordinates: Array(2), type: "Point"}
place_name: "🚙 Aeroplane Track, Toolangi VIC, Australia"
properties:
annotations: {DMS: {…}, MGRS: "55HCU6878852327", Maidenhead: "QF22sm18wa", Mercator: {…}, OSM: {…}, …}
bounds: {northeast: {…}, southwest: {…}}
components: {ISO_3166-1_alpha-2: "AU", ISO_3166-1_alpha-3: "AUS", _type: "road", continent: "Oceania", country: "Australia", …}
confidence: 7
formatted: "Aeroplane Track, Toolangi VIC, Australia"
__proto__: Object
type: "Feature"
__proto__: Object
length: 1
__proto__: Array(0)


I am able to get results of the geocode to appear correctly (in the same format as the local data version) in the console (console.log(matchingFeatures)) but the subsequent return matchingFeatures doesnt seem to work to add them to the mapbox geocoder results.



Been searching around for days with no luck. Annoyingly I feel like this is a common problem as the Mapbox geocoder results are lacking in many ways.



Any guidance would be very much appreciated.









share



























    0















    I am still learning JS so please bear with me.



    I have built a Mapbox GL JS map and would like to supplement Mapbox geocoding results with those from an external geocoding API as many POIs I need are not returned with the standard Mapbox geocoder (such as campsites and national parks).



    I have followed the tutorial to supplement results by querying local data here: Supplement forward geocoding search results from another data source



    I have got this to work with local a geojson source however I cant get it to work when querying an external geocoder. I am testing with OpenCage.



    My code for the geocoder instance on the map is:



    // Load geojson source data                
    var myjson;
    $.getJSON("<github hosted file>", function(json){
    myjson = json;
    });

    // Local geocoder function
    function forwardGeocoder(query) {

    var matchingFeatures = [];
    for (var i = 0; i < myjson.features.length; i++) {
    var feature = myjson.features[i];
    if (feature.properties.road_name.toLowerCase().search(query.toLowerCase()) !== -1) {
    feature['place_name'] = '🚙 ' + feature.properties.road_name;
    feature['center'] = feature.geometry.coordinates;
    matchingFeatures.push(feature);
    }
    }

    return matchingFeatures;
    }

    //Add geocoder/search box
    var geocoder = new MapboxGeocoder({
    accessToken: mapboxgl.accessToken,
    localGeocoder: forwardGeocoder,
    country: 'au'
    });


    This works fine for local data pulled in from a geojson source (i.e. already loaded in the client side). When I try to add an external geocoder into the forwardGeocoder() function, the query returns the correct geocoded results and I can get them to appear in the console but they dont appear in the standard mapbox geocoder results list. Code for the external geocoder (using OpenCage to test).



    function forwardGeocoder(query){
    var matchingFeatures = [];
    $.ajax({
    url: 'https://api.opencagedata.com/geocode/v1/geojson',
    method: 'GET',
    data: {
    'key': 'ab6ad6e3ad3043e2aeb897322ab038f0',
    'q': query,
    'pretty': 1
    },
    dataType: 'json',
    statusCode: {
    200: function(response){ // success
    var listings = response.features;
    //console.log(listings);
    for (var i = 0; i < listings.length; i++) {
    var feature = listings[i];
    if (feature.properties.formatted.toLowerCase().search(query.toLowerCase()) !== -1) {
    feature['place_name'] = '🚙 ' + feature.properties.formatted;
    feature['center'] = feature.geometry.coordinates;
    matchingFeatures.push(feature);
    }
    }
    console.log(matchingFeatures);
    return matchingFeatures;
    },
    402: function(){
    console.log('hit free-trial daily limit');
    console.log('become a customer: https://opencagedata.com/pricing');
    }
    }
    });
    }


    Console output from this function:



    0:
    center: (2) [145.516244, -37.4666249]
    geometry: {coordinates: Array(2), type: "Point"}
    place_name: "🚙 Aeroplane Track, Toolangi VIC, Australia"
    properties:
    annotations: {DMS: {…}, MGRS: "55HCU6878852327", Maidenhead: "QF22sm18wa", Mercator: {…}, OSM: {…}, …}
    bounds: {northeast: {…}, southwest: {…}}
    components: {ISO_3166-1_alpha-2: "AU", ISO_3166-1_alpha-3: "AUS", _type: "road", continent: "Oceania", country: "Australia", …}
    confidence: 7
    formatted: "Aeroplane Track, Toolangi VIC, Australia"
    __proto__: Object
    type: "Feature"
    __proto__: Object
    length: 1
    __proto__: Array(0)


    I am able to get results of the geocode to appear correctly (in the same format as the local data version) in the console (console.log(matchingFeatures)) but the subsequent return matchingFeatures doesnt seem to work to add them to the mapbox geocoder results.



    Been searching around for days with no luck. Annoyingly I feel like this is a common problem as the Mapbox geocoder results are lacking in many ways.



    Any guidance would be very much appreciated.









    share

























      0












      0








      0








      I am still learning JS so please bear with me.



      I have built a Mapbox GL JS map and would like to supplement Mapbox geocoding results with those from an external geocoding API as many POIs I need are not returned with the standard Mapbox geocoder (such as campsites and national parks).



      I have followed the tutorial to supplement results by querying local data here: Supplement forward geocoding search results from another data source



      I have got this to work with local a geojson source however I cant get it to work when querying an external geocoder. I am testing with OpenCage.



      My code for the geocoder instance on the map is:



      // Load geojson source data                
      var myjson;
      $.getJSON("<github hosted file>", function(json){
      myjson = json;
      });

      // Local geocoder function
      function forwardGeocoder(query) {

      var matchingFeatures = [];
      for (var i = 0; i < myjson.features.length; i++) {
      var feature = myjson.features[i];
      if (feature.properties.road_name.toLowerCase().search(query.toLowerCase()) !== -1) {
      feature['place_name'] = '🚙 ' + feature.properties.road_name;
      feature['center'] = feature.geometry.coordinates;
      matchingFeatures.push(feature);
      }
      }

      return matchingFeatures;
      }

      //Add geocoder/search box
      var geocoder = new MapboxGeocoder({
      accessToken: mapboxgl.accessToken,
      localGeocoder: forwardGeocoder,
      country: 'au'
      });


      This works fine for local data pulled in from a geojson source (i.e. already loaded in the client side). When I try to add an external geocoder into the forwardGeocoder() function, the query returns the correct geocoded results and I can get them to appear in the console but they dont appear in the standard mapbox geocoder results list. Code for the external geocoder (using OpenCage to test).



      function forwardGeocoder(query){
      var matchingFeatures = [];
      $.ajax({
      url: 'https://api.opencagedata.com/geocode/v1/geojson',
      method: 'GET',
      data: {
      'key': 'ab6ad6e3ad3043e2aeb897322ab038f0',
      'q': query,
      'pretty': 1
      },
      dataType: 'json',
      statusCode: {
      200: function(response){ // success
      var listings = response.features;
      //console.log(listings);
      for (var i = 0; i < listings.length; i++) {
      var feature = listings[i];
      if (feature.properties.formatted.toLowerCase().search(query.toLowerCase()) !== -1) {
      feature['place_name'] = '🚙 ' + feature.properties.formatted;
      feature['center'] = feature.geometry.coordinates;
      matchingFeatures.push(feature);
      }
      }
      console.log(matchingFeatures);
      return matchingFeatures;
      },
      402: function(){
      console.log('hit free-trial daily limit');
      console.log('become a customer: https://opencagedata.com/pricing');
      }
      }
      });
      }


      Console output from this function:



      0:
      center: (2) [145.516244, -37.4666249]
      geometry: {coordinates: Array(2), type: "Point"}
      place_name: "🚙 Aeroplane Track, Toolangi VIC, Australia"
      properties:
      annotations: {DMS: {…}, MGRS: "55HCU6878852327", Maidenhead: "QF22sm18wa", Mercator: {…}, OSM: {…}, …}
      bounds: {northeast: {…}, southwest: {…}}
      components: {ISO_3166-1_alpha-2: "AU", ISO_3166-1_alpha-3: "AUS", _type: "road", continent: "Oceania", country: "Australia", …}
      confidence: 7
      formatted: "Aeroplane Track, Toolangi VIC, Australia"
      __proto__: Object
      type: "Feature"
      __proto__: Object
      length: 1
      __proto__: Array(0)


      I am able to get results of the geocode to appear correctly (in the same format as the local data version) in the console (console.log(matchingFeatures)) but the subsequent return matchingFeatures doesnt seem to work to add them to the mapbox geocoder results.



      Been searching around for days with no luck. Annoyingly I feel like this is a common problem as the Mapbox geocoder results are lacking in many ways.



      Any guidance would be very much appreciated.









      share














      I am still learning JS so please bear with me.



      I have built a Mapbox GL JS map and would like to supplement Mapbox geocoding results with those from an external geocoding API as many POIs I need are not returned with the standard Mapbox geocoder (such as campsites and national parks).



      I have followed the tutorial to supplement results by querying local data here: Supplement forward geocoding search results from another data source



      I have got this to work with local a geojson source however I cant get it to work when querying an external geocoder. I am testing with OpenCage.



      My code for the geocoder instance on the map is:



      // Load geojson source data                
      var myjson;
      $.getJSON("<github hosted file>", function(json){
      myjson = json;
      });

      // Local geocoder function
      function forwardGeocoder(query) {

      var matchingFeatures = [];
      for (var i = 0; i < myjson.features.length; i++) {
      var feature = myjson.features[i];
      if (feature.properties.road_name.toLowerCase().search(query.toLowerCase()) !== -1) {
      feature['place_name'] = '🚙 ' + feature.properties.road_name;
      feature['center'] = feature.geometry.coordinates;
      matchingFeatures.push(feature);
      }
      }

      return matchingFeatures;
      }

      //Add geocoder/search box
      var geocoder = new MapboxGeocoder({
      accessToken: mapboxgl.accessToken,
      localGeocoder: forwardGeocoder,
      country: 'au'
      });


      This works fine for local data pulled in from a geojson source (i.e. already loaded in the client side). When I try to add an external geocoder into the forwardGeocoder() function, the query returns the correct geocoded results and I can get them to appear in the console but they dont appear in the standard mapbox geocoder results list. Code for the external geocoder (using OpenCage to test).



      function forwardGeocoder(query){
      var matchingFeatures = [];
      $.ajax({
      url: 'https://api.opencagedata.com/geocode/v1/geojson',
      method: 'GET',
      data: {
      'key': 'ab6ad6e3ad3043e2aeb897322ab038f0',
      'q': query,
      'pretty': 1
      },
      dataType: 'json',
      statusCode: {
      200: function(response){ // success
      var listings = response.features;
      //console.log(listings);
      for (var i = 0; i < listings.length; i++) {
      var feature = listings[i];
      if (feature.properties.formatted.toLowerCase().search(query.toLowerCase()) !== -1) {
      feature['place_name'] = '🚙 ' + feature.properties.formatted;
      feature['center'] = feature.geometry.coordinates;
      matchingFeatures.push(feature);
      }
      }
      console.log(matchingFeatures);
      return matchingFeatures;
      },
      402: function(){
      console.log('hit free-trial daily limit');
      console.log('become a customer: https://opencagedata.com/pricing');
      }
      }
      });
      }


      Console output from this function:



      0:
      center: (2) [145.516244, -37.4666249]
      geometry: {coordinates: Array(2), type: "Point"}
      place_name: "🚙 Aeroplane Track, Toolangi VIC, Australia"
      properties:
      annotations: {DMS: {…}, MGRS: "55HCU6878852327", Maidenhead: "QF22sm18wa", Mercator: {…}, OSM: {…}, …}
      bounds: {northeast: {…}, southwest: {…}}
      components: {ISO_3166-1_alpha-2: "AU", ISO_3166-1_alpha-3: "AUS", _type: "road", continent: "Oceania", country: "Australia", …}
      confidence: 7
      formatted: "Aeroplane Track, Toolangi VIC, Australia"
      __proto__: Object
      type: "Feature"
      __proto__: Object
      length: 1
      __proto__: Array(0)


      I am able to get results of the geocode to appear correctly (in the same format as the local data version) in the console (console.log(matchingFeatures)) but the subsequent return matchingFeatures doesnt seem to work to add them to the mapbox geocoder results.



      Been searching around for days with no luck. Annoyingly I feel like this is a common problem as the Mapbox geocoder results are lacking in many ways.



      Any guidance would be very much appreciated.







      geocoding mapbox point-of-interest





      share












      share










      share



      share










      asked 4 mins ago









      jnuccjnucc

      63




      63






















          0






          active

          oldest

          votes











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "79"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f313574%2fsupplement-mapbox-geocoder-results-with-external-geocoder-api%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f313574%2fsupplement-mapbox-geocoder-results-with-external-geocoder-api%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown