Hey, I'm trying to implent the Google Places Javascript API in Xamarin Forms (specifically the Autocomplete function) with XLabs.Forms.Controls.HybridWebView but the Javascript inside my HTML file wont run.
Here's my code:
public partial class Page1 : ContentPage
{
public String FormatedAddress;
public Page1 ()
{
InitializeComponent ();
HybridWebView webView = new HybridWebView(new JsonSerializer()) { VerticalOptions = LayoutOptions.FillAndExpand, HorizontalOptions = LayoutOptions.FillAndExpand };
string htmlString = @"<!DOCTYPE html>
<html lang=""en"" xmlns=""xxxx://www.w3.org/1999/xhtml"">
<head>
<meta charset = ""utf-8"" />
<title></title>
<script type = ""text/javascript"" src = ""xxxxx://maps.googleapis.com/maps/api/js?key=****************&libraries=places"" ></script>
</head>
<body>
<div id = ""locationField"">
<input id = ""autocomplete"" placeholder = ""Enter your address""
type = ""text""></input>
</div>
<br /><br />
<input class=""field"" id=""street_number"" disabled = ""true""></input>
<br /><br />
<button onclick=""javascript:testFunc();"">Click me</button>
<script type=""text/javascript"">
var autocomplete, finish;
function testFunc() {
document.getElementById('street_number').value = 'Im working!';
}
function initAutocomplete()
{
autocomplete = new google.maps.places.Autocomplete(
/** @type {!HTMLInputElement} */(document.getElementById('autocomplete')),
{ types: ['address']
});
autocomplete.addListener('place_changed', formateAddress);
}
function SendToCS()
{
Native('addressCallback', finish);
}
function formateAddress()
{
var place = autocomplete.getPlace();
finish = place.formatted_address;
SendToCS();
}
</script>
<script src = ""xxxxx://maps.googleapis.com/maps/api/js?key=*************************&libraries=places&callback=initAutocomplete""
async defer></script>
</body>
</html>";
webView.Source = new HtmlWebViewSource { Html = htmlString };
this.Content = webView;
webView.RegisterCallback("addressCallback", CalledFromJS);
}
private void CalledFromJS(String address)
{
FormatedAddress = String.Copy(address);
}
So as you can see, I was a bit lazy hardcoding the HTML/JS stuff and I also implemented some C#-Callback stuff but the problem starts even before that.
I implemented a simple testFunc() in JS which should be called when pressing the Click me button but nothing happens when pressing the button so I'm assuming my Javascript Code just wont be executed.
So all in all I'm searching for a simple solution to execute my HTML/JS Webpage and not just a solution for calling a specific JS method once.