Get value from javascript in template

Hi
I have found some examples using Google places API to autocomplete an address.
And I want to get the lat / lon from the returned result.
I'm struggling with getting the lat/lon values back in msg.payload.xxx.
I can fine see the values in "console.log" (F12), but how do I get this console.log into something i can process.

[{"id":"326bdf59fd90dd51","type":"template","z":"bd6851c447e9acc8","name":"JavaScript","field":"payload.script","fieldType":"msg","format":"javascript","syntax":"plain","template":"// This sample requires the Places library. Include the libraries=places\n// parameter when you first load the API. For example:\n// <script\n// src=\"https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places\">\nfunction initMap() {\n    const node = this;\n    const map = new google.maps.Map(document.getElementById(\"map\"), {\n        center: { lat: -33.8688, lng: 151.2195 },\n        zoom: 13,\n    });\n    const input = document.getElementById(\"pac-input\");\n    // Specify just the place data fields that you need.\n    const autocomplete = new google.maps.places.Autocomplete(input, {\n        fields: [\"place_id\", \"geometry\", \"name\", \"formatted_address\"],\n    });\n\n    autocomplete.bindTo(\"bounds\", map);\n    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);\n\n    const infowindow = new google.maps.InfoWindow();\n    const infowindowContent = document.getElementById(\"infowindow-content\");\n\n    infowindow.setContent(infowindowContent);\n\n    const geocoder = new google.maps.Geocoder();\n    const marker = new google.maps.Marker({ map: map });\n\n    marker.addListener(\"click\", () => {\n        infowindow.open(map, marker);\n    });\n    autocomplete.addListener(\"place_changed\", () => {\n        infowindow.close();\n\n        const place = autocomplete.getPlace();\n\n        if (!place.place_id) {\n            return;\n        }\n\n        geocoder\n            .geocode({ placeId: place.place_id })\n            .then(({ results }) => {\n                map.setZoom(11);\n                map.setCenter(results[0].geometry.location);\n                // Set the position of the marker using the place ID and location.\n                // @ts-ignore TODO This should be in @typings/googlemaps.\n                marker.setPlace({\n                    placeId: place.place_id,\n                    location: results[0].geometry.location,\n                });\n                marker.setVisible(true);\n                infowindowContent.children[\"place-name\"].textContent = place.name;\n                infowindowContent.children[\"place-id\"].textContent = place.id;\n                infowindowContent.children[\"place-address\"].textContent =\n                    results[0].formatted_address;\n                infowindow.open(map, marker);\n\n                // Here you can get your coordinates like this\n                console.log(place.geometry.location.lat());\n                console.log(place.geometry.location.lng());\n                console.log(\"Test\");\n                \n            })\n            .catch((e) => window.alert(\"Geocoder failed due to: \" + e));\n    });\n}\n\nwindow.initMap = initMap;","output":"str","x":1106.0000534057617,"y":858,"wires":[["ad0566d863a022b4"]]}]

Google places API is a paid service for which you need an API key - if this would be possible without it, there wouldn't be much of a business case.

Can you show us the output of your function?

Background, I want to use a MAP API, where you can see the map, but also with "Satelite layer".
My hope is to make this "app" in NR, where you can point on map, or input the address, and then get lat/lon returned, so I can send that info and some other to a navigation unit .
So Google was my first choise, because of the examples.
Others stuff I'm trying is LeafLet, with different combination Geoapify, LocationIQ, Nominatim and what I else can find.

My example is based of an combination of this and then the autocomplete from google
with some small modifications.
I don't have any "funktions" just the flow from first ex.

So I'm mostly looking for howto get (concole.log) in NR flow and not only in browser log.

@bakman2 - yes I know I need a API key (witch I have), but from my understanding, it's free when you only use address/position info.
And also when this is up and running - we then talked 1-100 req. pr. month.

You could use the web sockets to link between node-red and the client side Javascript.
This example flow should help.

I will look into that, seems like it's just what I'm look for.
Thanks

You could also possible use fetch client side to send a http request to a node-red endpoint.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.