I'm trying to create a dynamic InputField
that would autocomplete the address as I type it into the field, by calling Google Maps' GeoCoding API on every change of the field's value.
How can I make AJAX-style HTTP calls when the value of the InputField
changes?
So for the only approach that I'm aware of is by using UpValues
: Detecting changes of variables' values
Here's my code using UpValues
:
DynamicModule[{addr, geo},
addr /: Set[addr, x_] := (
(
OwnValues[addr] = {HoldPattern[addr] :> x};
Print["Value changed"];
geo = (Import["https://maps.googleapis.com/maps/api/geocode/json",
"JSON", "RequestParameters" -> {"address" -> x}] /.
List -> Association)["results"]
);
addr
);
addr = "555 Mission St, San Francisco";
Column@
{
InputField[Dynamic[addr], String, ContinuousAction -> True],
Dynamic[addr], Dynamic[geo["formatted_address"]]
}
]
It kind of works, but for some reason not every change of the field's value triggers an API call.
Hence two questions:
Why are some of the change events lost?
Is there a better way to do AJAX-style HTTP calls when the value of a dynamic variable changes?
Also, these API calls via Import
take ~200 ms, while they take much less time if I make them in a browser. Is there any way to speed them up?