Geolocation

by Ryan Brackett

HTML

<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=getLocation"></script>

<script src="https://cdn.kyruus.com/lib/kyruus-search-widget/version/2/kyruus-search-widget.min.js"></script>

<script type="text/javascript">
  $(document).ready(function() {
    //KYRUUS SEARCH WIDGET
    (function() {
      var kyruusSearchWidget = new KyruusSearchWidget({
        pm_url: 'https://doctors.nghs.com',
        el: document.getElementById('kyruus-search-widget'),
        customer_code: 'nghs',
        search_label: 'Search',
        message: 'ex: Primary Care',
        show_location: true,
        location_search_radius: 10,
        show_powered_by: false,
        typeahead_categories: [{
            "category": "clinical_experience"
          },
          {
            "category": "specialty_synonym"
          },
          {
            "category": "name"
          },
          {
            "category": "primary_care"
          }
        ],
        allow_empty_search: true,
        focus_on_widget: true,
        use_primary_care_condition: true,
        persist_query: true,
        legal_message: 'off',
        sort_order: 'relevance, networks',
      });
    }());


    $(".ky-input-location").append("<div id='current-location'><span class='material-icons'>location_on</span> Get my current location</div>");

    $("#current-location").on('click', function() {
      console.log("Get location");

    });


    $("#location").attr("placeholder", "City, State or Zip");
    $("#query").blur();
  });

</script>

JavaScript

function getLocation() {
  geocoder = new google.maps.Geocoder();

  // Try HTML5 geolocation.
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var pos = {
        lat: position.coords.latitude,
        lng: position.coords.longitude
      };

      geocoder.geocode({
        'location': pos
      }, function(results, status) {
        if (status == 'OK') {

          var cityName = results[0]["address_components"][2]["long_name"];
          var stateName = results[0]["address_components"][4]["short_name"]
					var locationFormatted = cityName + ", " + stateName;
          console.log(locationFormatted);

        } else {
          console.log('Geocode was not successful for the following reason: ' + status);
        }
      });


    }, function() {
      handleLocationError(true);
    });
  } else {
    // Browser doesn't support Geolocation
    handleLocationError(false);
  }
}