call google api as a function in my search widget

2024
3
Jump to solution
07-09-2017 06:38 AM
Henryobaseki
Occasional Contributor

Hi All,

Do not know if anyone has worked with Google places api or OSplaces api.all similar to Esri javascript api.

I would like to include Google search places api as a function or sources in my search widget code. Don't really know the best way to go about this though.

Google Api search function

https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple

<script>
     
function initMap() {
       
var map = new google.maps.Map(document.getElementById('map'), {
          zoom
: 8,
          center
: {lat: -34.397, lng: 150.644}
       
});
       
var geocoder = new google.maps.Geocoder();

        document
.getElementById('submit').addEventListener('click', function() {
          geocodeAddress
(geocoder, map);
       
});
     
}

     
function geocodeAddress(geocoder, resultsMap) {
       
var address = document.getElementById('address').value;
        geocoder
.geocode({'address': address}, function(results, status) {
         
if (status === 'OK') {
            resultsMap
.setCenter(results[0].geometry.location);
           
var marker = new google.maps.Marker({
              map
: resultsMap,
              position
: results[0].geometry.location
           
});
         
} else {
            alert
('Geocode was not successful for the following reason: ' + status);
         
}
       
});
     
}
   
</script

I want the google search api to carry out the address searches when a user click on a search button to search a post code for example but I want the search result to be displaced on ESri map.

How do I include google api search to the searchWidget sample below

ESRI Search Widget( its also a geocoding service on its own)

<script>
    require([
      "esri/Map",
      "esri/views/SceneView",
      "esri/widgets/Search",
      "dojo/domReady!"
    ], function(
      Map,
      SceneView,
      Search) {
      var map = new Map({
        basemap: "satellite",
        ground: "world-elevation"
      });
      var view = new SceneView({
        scale: 123456789,
        container: "viewDiv",
        map: map
      });
      var searchWidget = new Search({
        view: view
      });
      // Add the search widget to the very top left corner of the view
      view.ui.add(searchWidget, {
        position: "top-left",
        index: 0
      });
    });
  </script>
The other option will be to the  search widget multiple sources option, how do I call the google api function on its on without including google map function initMap.
Thanks for all your help...
0 Kudos
1 Solution

Accepted Solutions
Henryobaseki
Occasional Contributor

Hi All

It just tool complicated to use external service within Esri JavaScript API. I have opted for Esri geocoding service instead it gives me what I need an xy coordinates with addresses.

Thanks...

View solution in original post

0 Kudos
3 Replies
ThomasSolow
Occasional Contributor III

You should be able to use google's API without pulling anything in from google.  It's been a while since I've used a google API but a quick search led me to a couple sample URLs:

https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY
https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY

(source)

You can make requests in these formats using an XHR, or whichever wrapper your prefer (esri/request is an option since you're pulling in the Esri JS API) and get back a list of results, which you can then display how you choose.

I recommend creating your own search widget: you can add a text box, search button, and a place to display results.

Henryobaseki
Occasional Contributor

The json format you showed is for one entity or location.

I do have a search widget jean Marc Roy helped with this; see link below

https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fplnkr.co%2Fedit%2FcJr3F4OWjVeseUf8Lj...

====================================================================================================

Searches coming from different featureservers.

var featureSource1 = new FeatureLayer({
   url: "https://services7.arcgis.com/uE69mPI6U3rpSmCT/arcgis/rest/services/aAdd_csv/FeatureServer/0",
   popupTemplate: { // autocasts as new popupTemplate()
    title: "Post Code: ${Postcode}</br>UPRN: ${UPRN}</br>ADDRESS: ${ADDRESS}",
    overwriteActions: true
   }
  })
  var featureSource2 = new FeatureLayer({
   url: "https://services7.arcgis.com/uE69mPI6U3rpSmCT/arcgis/rest/services/postcodesS/FeatureServer/0",
   popupTemplate: { // autocasts as new popupTemplate()
    title: "Post Code: ${Postcode_3}</br>Wards Name: ${Ward_Name}</br>Ward: ${Ward_Code}",
    overwriteActions: true
   }

============================================================================================================

I would like my third source, if possible would like the only source to come from googleApi. I guess when a user types in an address in the search it returns the json format as a lookup and then displaces it on the map.

==================================================================================================================

here is my google map key

https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+...

Thanks in advance

0 Kudos
Henryobaseki
Occasional Contributor

Hi All

It just tool complicated to use external service within Esri JavaScript API. I have opted for Esri geocoding service instead it gives me what I need an xy coordinates with addresses.

Thanks...

0 Kudos