Hi David and Sean-
THANK YOU so much for all your help. Yes, ultimately, I was missing the "navigator.geolocation.getCurrentPosition" function, which I've read more about, after getting your notes. What I was trying to do, works great.
I kept the code very simple: I wanted the user to easily display the current location, on a map and I've expanded it to create a route from the Current Location to an address that they can enter. The code is placed in the onClick function of a Button.
Here's the code, I used, in case it will help someone else accomplish this task easily:
I created two TextBox fields, called TextBox1 (hidden) and TextBox2 (visible: to enter the destination address). And, of course, there's a button to activate the script, below:
//This function runs if the Location Service is on and the GPS position can be determined
var Success2 = function(setfields) {
//Set the Longitude and Latitude field variables, based on the current location
var lat = setfields.coords.latitude;
var long = setfields.coords.longitude;
//Then, I created a single field that holds both numbers, with a comma separator and put that into the hidden TextBox1.
var myPosition = lat + ','+ long
{dialog.Object}.setValue('TEXTBOX1',myPosition);
}
//In the event of an error getting the coordinates, this will tell me the error that occurred.
function onError2(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
//This pulls the Navigation coordinates from the device and runs the function(s)
navigator.geolocation.getCurrentPosition(Success2, onError2);
//Finally, the fun part: After the user enters an address into TextBox2 the system and clicks the Button, this runs:
//Get the Values from both TextBoxes show the Route in a "text field' (that's designated as a DIV)
var a1 = {dialog.Object}.getValue('textbox1');
var a2 = {dialog.Object}.getValue('textbox2');
var m1 = {dialog.object}.getControl('ROUTE1');
m1.plotRoute(a1,a2);
var Success2 = function(setfields) {
//Set the Longitude and Latitude field variables, based on the current location
var lat = setfields.coords.latitude;
var long = setfields.coords.longitude;
//Then, I created a single field that holds both numbers, with a comma separator and put that into the hidden TextBox1.
var myPosition = lat + ','+ long
{dialog.Object}.setValue('TEXTBOX1',myPosition);
}
//In the event of an error getting the coordinates, this will tell me the error that occurred.
function onError2(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
//This pulls the Navigation coordinates from the device and runs the function(s)
navigator.geolocation.getCurrentPosition(Success2, onError2);
//Finally, the fun part: After the user enters an address into TextBox2 the system and clicks the Button, this runs:
//Get the Values from both TextBoxes show the Route in a "text field' (that's designated as a DIV)
var a1 = {dialog.Object}.getValue('textbox1');
var a2 = {dialog.Object}.getValue('textbox2');
var m1 = {dialog.object}.getControl('ROUTE1');
m1.plotRoute(a1,a2);
I'd still like to test it a little, but, so far, it has been perfect... and I couldn't have done it, without your help. I'm also going to try some of the options (accuracy, etc.) to see if it makes a difference.
I hope that code is helpful for someone else who wants to do something similar.
Thanks again for all your help. I really appreciate it.
Leave a comment: