Hiya Leland -- thank you for helping!!
I’m using the google maps geocoding api (and the boilerplate code from here) and the results come back as this object
results[]: {
types[]: string,
formatted_address: string,
address_components[]: {
short_name: string,
long_name: string,
postcode_localities[]: string,
types[]: string
},
partial_match: boolean,
place_id: string,
postcode_localities[]: string,
geometry: {
location: LatLng,
location_type: GeocoderLocationType
viewport: LatLngBounds,
bounds: LatLngBounds
}
}
It’s the first time I’ve worked with the geocoding api, so I set it up on this test page to recenter the map and it has it’s own submit button.
As the interim step, I’ve been trying to capture the values when the results object is returned. But all I get is ?lat=&lon= . Here’s the code:
<script>
var geocoder;
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: {lat: 42.3853, lng: -71.8368}
});
{% for row in hubdb_table_rows(table_id, filter_params) %}
var marker_{{ row.hs_id }} = new google.maps.Marker({
map: map,
position: {lat: {{ row.location.lat }}, lng: {{ row.location.lon }} },
icon: { scaledSize: {width: 20, height:20}, url: "https://cdn2.hubspot.net/hubfs/4764394/01012019_ARC_icon_svgs/ARC_map_150px_150px_navy_02.png" },
title: "{{ row.organization }}",
});
marker_{{ row.hs_id }}.addListener('click', function() {
new google.maps.InfoWindow({
content: "<div> {{ row.organization }} </div>"
}).open(map, marker_{{ row.hs_id }});
});
{% endfor %}
geocoder = new google.maps.Geocoder();
document.getElementById('address-submit').addEventListener('click', function() {
codeAddress(geocoder, map);
});
function codeAddress(geocoder, resultsMap) {
var address = document.getElementById('address').value;
geocoder.geocode( {'address' : address}, function(results, status) {
if (status == 'OK') {
resultsMap.setCenter(results[0].geometry.location);
{% set lat_param = lat_param ~ "&latitude=" + results[0].geometry.location.lat|urlencode %}
{% set lon_param = lon_param ~ "&longitude=" + results[0].geometry.location.lat|urlencode %}
var marker_origin = new google.maps.Marker({
map: resultsMap,
title: "You are here.",
icon: { scaledSize: {width:25, height:25}, url: "https://cdn2.hubspot.net/hubfs/4764394/01012019_ARC_icon_svgs/ARC_map_150px_150px_green_02.png"},
position: results[0].geometry.location,
zoom: 14,
});
} else {
alert('Geocode was not successful for the following reason: ' + status)
}
});
}
}
</script>
I figured once I was able to do that, then I could rewrite to the full feature which should use one submit button, so people could filter on the additional options too, and then the return would recenter the map on their address and sort the facility cards by closest first.
Does this help you help me? What else can I provide? Should I add you to the project so you can look at my (super-messy, unrefactored) module?