Retrieve data from webView

Hi,

I am showing a google map in a Corona web view and I’d like to retrieve the coordinates where the user clicks on the map.

Is there a way to force the urlRequest listener with javascript and to pas extra parameters to it so it I can for instance access to event.lat or event.lon

Maybe with an ajax call??

Thanks!

Ok, found a way that made the trick but it’s not what I really wanted.

In case somebody needs it:

Change the URL adding params and force page to reload to call the listener

See:

main.lua

[lua]

local view=native.newWebView(0,0,320,480)

local function listener(event)

    print(event.type)

    print(event.url)

end

view:addEventListener(“urlRequest”,listener)

view:request(“hello.html”, system.ResourceDirectory)

[/lua]

hello.html

[lua]

<script>

function myFunction() {

    window.location.assign(document.URL+"?a=34")

}

</script>

<button onclick=“myFunction()”>Click me</button>

[/lua]

Ok, found a way that made the trick but it’s not what I really wanted.

In case somebody needs it:

Change the URL adding params and force page to reload to call the listener

See:

main.lua

[lua]

local view=native.newWebView(0,0,320,480)

local function listener(event)

    print(event.type)

    print(event.url)

end

view:addEventListener(“urlRequest”,listener)

view:request(“hello.html”, system.ResourceDirectory)

[/lua]

hello.html

[lua]

<script>

function myFunction() {

    window.location.assign(document.URL+"?a=34")

}

</script>

<button onclick=“myFunction()”>Click me</button>

[/lua]

Hi, I just saw what you were doing above and it does make sense. So did you find a way to pass the coordinates? I see you are passing a=34 but is there any way to pass the coordinates in there? What I really want to do is pass a selected marker or some sort of ID for that marker as the parameter. Do you know how to do that? I plan to add several markers to it but do not want the popup to show when you click on a marker. Thats where I want to pass the marker’s ID as a parameter and show my own popup with the marker’s information that I have.

Thanks,

Warren

Another variant of igenapps solution wherein you do not wish to wait for a button click is to have the html file as follows:

\<html\> \<head\> \<script\> var myValueToPassBack = 'someValue'; window.onload = function() { if(!window.location.hash) { window.location = window.location + '#loaded#' + myValueToPassBack; window.location.reload(); } } \</script\> \</head\> \<body\> Hello World! \</body\> \</html\>

This loads the file, the reload fires once, but not the second time as #loaded#someValue gets suffixed to the URL. 

The event listener in main.lua is able to capture the reload event coming from the newWebView or showWebPopup.

@warrenwsav yes you can pass any parameters you want and when you want. Just change the event that will trigger the url to be changed and write the parameters you need in the url. It is very helpful to pass json strings, this way you can parse them into lua tables in the Corona side. What I wrote above was just an example showing the logic behind it but it is very flexible

Hi, I just saw what you were doing above and it does make sense. So did you find a way to pass the coordinates? I see you are passing a=34 but is there any way to pass the coordinates in there? What I really want to do is pass a selected marker or some sort of ID for that marker as the parameter. Do you know how to do that? I plan to add several markers to it but do not want the popup to show when you click on a marker. Thats where I want to pass the marker’s ID as a parameter and show my own popup with the marker’s information that I have.

Thanks,

Warren

Another variant of igenapps solution wherein you do not wish to wait for a button click is to have the html file as follows:

\<html\> \<head\> \<script\> var myValueToPassBack = 'someValue'; window.onload = function() { if(!window.location.hash) { window.location = window.location + '#loaded#' + myValueToPassBack; window.location.reload(); } } \</script\> \</head\> \<body\> Hello World! \</body\> \</html\>

This loads the file, the reload fires once, but not the second time as #loaded#someValue gets suffixed to the URL. 

The event listener in main.lua is able to capture the reload event coming from the newWebView or showWebPopup.

@warrenwsav yes you can pass any parameters you want and when you want. Just change the event that will trigger the url to be changed and write the parameters you need in the url. It is very helpful to pass json strings, this way you can parse them into lua tables in the Corona side. What I wrote above was just an example showing the logic behind it but it is very flexible