Hi,
this code works with me.
In the backend file, make this .jsw file
this file has a function takes the address and return the latitude, longitude and more…
import {fetch} from ‘wix-fetch’;
const key = “AIzaSyCEzAmnS6GFBmXbNqLePd4d2TLsgmeGdZg”;
const GEOKEY = “AIzaSyCI7NI4E3sNKbzQKZkAbz-ZTBbgqEiMDaY”;
const apart1 = “https://maps.googleapis.com/maps/api/place/autocomplete/json?”;
const apart2 = “&types=address&key=”;
export function autocomplete(string) {
let input = “input=” + string;
let url = apart1 + input + apart2 + key;
return fetch (url, {method: ‘get’}).then( (httpResponse) => {
if (httpResponse.ok) {
return httpResponse.json();
}
});
}
const dpart1 = “https://maps.googleapis.com/maps/api/place/details/json?”;
const dpart2 = “&key=”;
export function details(id) {
let placeid = “placeid=” + id;
let url = dpart1 + placeid + dpart2 + key;
return fetch (url, {method: ‘get’}).then( (httpResponse) => {
if (httpResponse.ok) {
return httpResponse.json();
}
});
}
const geocode1 = “https://maps.googleapis.com/maps/api/geocode/json?address=”;
const geocode2 = “&key=”;
export function geocodeAddress(AddressName) {
let geoUrl = geocode1 + AddressName + geocode2 + GEOKEY;
return fetch (geoUrl, {method: ‘get’}).then( (httpResponse) => {
if (httpResponse.ok) {
return httpResponse.json();
}
});
}
in the page that has the map…
import {geocodeAddress} from ‘backend/gapi’;
and use this (change the address when you call the function):
geocodeAddress(fullAddress).then((json) => {
// fills in remainder of address from query
for(var i in json.results[0].address_components){
if (JSON.stringify(json.results[0].address_components[i].types).indexOf(“administrative_area_level_1”) !== -1)
{ var state = json.results[0].address_components[i].long_name;}
if (JSON.stringify(json.results[0].address_components[i].types).indexOf(“locality”) !== -1)
{ var city = json.results[0].address_components[i].long_name;}
if (JSON.stringify(json.results[0].address_components[i].types).indexOf(“street_number”) !== -1)
{ var street = json.results[0].address_components[i].long_name;}
if (JSON.stringify(json.results[0].address_components[i].types).indexOf(“route”) !== -1){
if (street === undefined )
{street= json.results[0].address_components[i].long_name;} else
{street = street + " " + json.results[0].address_components[i].long_name;}}
if (JSON.stringify(json.results[0].address_components[i].types).indexOf(“postal_code”) !== -1)
{ var zipcode = json.results[0].address_components[i].long_name;}
var mapaddress = street + ', ’ + city + ', ’ + state + ', ’ + zipcode;
$w('#propertyLocation').location = {
“latitude”: Number(json.results[0].geometry.location.lat),
“longitude”: Number(json.results[0].geometry.location.lng),
“description”:mapaddress,
};
}
});