UNPKG

react-autocomplete-places

Version:
73 lines (68 loc) 2.12 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var geocodeByAddress = exports.geocodeByAddress = function geocodeByAddress(address) { var geocoder = new window.google.maps.Geocoder(); var OK = window.google.maps.GeocoderStatus.OK; return new Promise(function (resolve, reject) { geocoder.geocode({ address: address }, function (results, status) { if (status !== OK) { reject(status); } resolve(results); }); }); }; var getLatLng = exports.getLatLng = function getLatLng(result) { return new Promise(function (resolve, reject) { try { var latLng = { lat: typeof result.geometry.location.lat === 'function' ? result.geometry.location.lat() : result.geometry.location.lat, lng: typeof result.geometry.location.lng === 'function' ? result.geometry.location.lng() : result.geometry.location.lng }; resolve(latLng); } catch (e) { reject(e); } }); }; var geocodeByPlaceId = exports.geocodeByPlaceId = function geocodeByPlaceId(placeId, getPlaceUrl) { var geocoder = new window.google.maps.Geocoder(); var OK = window.google.maps.GeocoderStatus.OK; return new Promise(function (resolve, reject) { var url = getPlaceUrl; if (!url) { geocoder.geocode({ placeId: placeId }, function (results, status) { if (status !== OK) { reject(status); } resolve(results); }); return; } fetch(url).then(function (response) { return response.json(); }).then(function (data) { if (data.status == 'OK') { var result = []; result.push(data.result); resolve(result); } else { geocoder.geocode({ placeId: placeId }, function (results, status) { if (status !== OK) { reject(status); } resolve(results); }); } }).catch(function () { geocoder.geocode({ placeId: placeId }, function (results, status) { if (status !== OK) { reject(status); } resolve(results); }); }); }); };