iOS, Google Maps and GPS, Oh My!

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
josephreesGA75ef
Posts: 22
Joined: Mon Dec 05, 2011 5:33 pm

iOS, Google Maps and GPS, Oh My!

Post by josephreesGA75ef » Mon Dec 12, 2011 6:51 am

So, folks, I could use some assistance here. I've run into a couple of issues that I can't seem to work around with the beginnings of an app I'm trying to write. With the help of another forum poster, I was able to get an app running that will use GPS/Location Services to show my current location on a Google Map with an iOS browser control. However, I have some issues to resolve which I can't seem to figure out.

Firstly, I currently have the personal LC license with the iOS option and I am almost suspecting that not having the commercial license is to blame (or a bug in LC or my code somewhere). I've discovered that when you activate GPS with the iphoneStartTrackingLocation command, it takes a couple of seconds to lock onto your position (kind of expected, really). If you populate your variable from the iphoneCurrentLocation command too soon, your coordinates will end up being 0,0 because the key/value pair is not in existence yet. I'm trying to write some code that will wait until the current location has been received, and is not null or "0" before trying to do anything else. I've managed a small workaround while debugging, but it's not very good and has a nasty side effect. I've simply added "wait for 2 seconds" right after I start the location tracking, and before populating the results into a variable. However, no matter where I seem to put this code (stack, card, etc.) the "This was built with a Live Code Personal license, not for commercial distribution" splash screen just sticks around for an extra 2 seconds before going away. I've played with putting it in the preOpenStack, openStack, preOpenCard, openCard, etc. Every place I put this code, just makes the default LC splash screen just hang around longer. With this method, at least the variable I defined gets populated with legitimate GPS coordinates and I can get the Map to display with the correct location shown.

I really want to do my own splash screen (separate card) with a fake modal window (unless there is a way to do a real iOS-like modal popup, other than the "answer" dialog - I'm not seeing anything like that in MobGUI though) that says something like "Acquiring GPS Location" and runs a loop in the background continuously checking to see if tLocation["latitude"] is either NOT 0 or NOT null. Once I have populated the variable using "put iphoneCurrentLocation into tLocation", it's set with whatever iphoneCurrentLocation returns, even if it's nothing. I planned to work around that by performing the "put iphoneCurrentLocation into tLocation" command again in the loop.

The problem is, I can't seem to get the loop logic to work at all. When I do manage to get the loop to function, I get into an endless loop and my CPU spikes to 100%. It also never finds my location and leaves me trapped at the LC splash screen forever. I've tried it a multitude of different ways with no success. Sometimes I can't even get the loop code to function at all and the code just seems to never be evaluated, skipping my check/loop completely. I went back to basics and used some the EXACT code used in the LC dictionary, and couldn't even get that to work for me. A simple loop like below wouldn't work and this was literally copy/pasted from the dictionary. I never got an answer dialog at all.

Code: Select all

repeat with x = 1 to 5
   answer x
end repeat
I tried to fight my way around any kind of waiting times in the main stack causing the LC splash screen from sticking around by making the main stack's preOpenStack code "go next card" to display my first card (i.e. splash screen) which begins finding the current location and the waiting sequence. Then once it has been found, populate the tLocation variable (I made it a global) and "go next card" again to take me to the card which shows the map. However, even doing it this way and without the loop code, just the "wait for 2 seconds", I still get hung up at the LC splash screen instead of showing the card I want.

Can anyone offer any suggestions for how to get my loop that checks to see if tLocation["latitude"] has populated functioning correctly and avoid causing the LC splash screen from sticking around?

------------------------

Problems 2 & 3:

I wanted to play with live tracking of the current location. There is a default locationChanged command that I actually have working, but I have 2 related problems. First, it seems that locationChanged has no parameters that can be used, it just does its' own thing. In the current fashion, even in the simulator with a static destination sent, it's refreshing the location approximately once per second or so. I can't wrap my head around this at all. If I have the location coordinates hard set in the simulator (Apple HQ), shouldn't this locationChanged message only be seen when I manually change the location? It happens so fast, that it's hard to actually change the location in the simulator and get the map to update. It's almost like the browser is just refreshing on a 1 second interval. When I do change the location and get it in at the right time, however, the map does update correctly and show the new location before it starts refreshing over and over on that particular location. Any ideas?

Related to this, If I send new coordinates to the HTML page with the locationChanged message, the browser control refreshes. Is there a way to do this with AJAX or something, so you can't see the "page" change? Does anyone have any experience with this?

Last thing, I promise. Does anyone have any samples of dropping a pin on a Google Map within iOS and LC?

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: iOS, Google Maps and GPS, Oh My!

Post by bn » Mon Dec 12, 2011 9:36 am

Hi Joseph,

if you issue a wait command in Livecode it effectively halts the engine. That is what you observerved.
However if you say "wait 3 seconds with messages" the engine can go on and your script resumes after 3 seconds. Maybe that would help.
I would go the way you planned. Go to a fake splash screen on a card. That way everything is set up on Livecode's side and you could wait for the GPS to start up.

In your loop that test whether GPS is up you could also add a "wait 150 milliseconds with messages" or so. No need to repeat tightly. This will also reduce the load on the processor.

Once you get the locationChanged message too often as you describe (have not played with it yet) you could test the data for change and only if it is not the same as before take action
You could do that by using either a global or a script local variable that holds the array of the last location and test against that data.
Is there a way to do this with AJAX or something, so you can't see the "page" change? Does anyone have any experience with this?
no idea, others may know.
Last thing, I promise. Does anyone have any samples of dropping a pin on a Google Map within iOS and LC?
no idea, others may know.

Kind regards

Bernd

josephreesGA75ef
Posts: 22
Joined: Mon Dec 05, 2011 5:33 pm

Re: iOS, Google Maps and GPS, Oh My!

Post by josephreesGA75ef » Mon Dec 12, 2011 11:33 am

Thanks for the help Bernd!

I tried the "wait x seconds with messages" and unfortunately had the same exact problem. I had thought of you solution to the locationChanged problem, and was going to institute it. So, tonight I did. I think what I've discovered now is that locationChanged doesn't seem to do what I think it should, based on the LiveCode documentation. Maybe this is something that has changed recently with iOS 5.0 or LC 5.0, not sure. I set up the logic you described, and here is my code:

Code: Select all

on locationChanged
   put iphoneCurrentLocation() into tNewLocation
   if round(tNewLocation["latitude"],1) is not round(tOldLocation["latitude"],1) then
      put tNewLocation["latitude"] into tLat
      put tNewLocation["longitude"] into tLong
      put specialFolderPath("documents") & "/lcNav.html" into thehtmlFile
      put URL("file:" & thehtmlFile) into theData
      put "var myLatlng = new google.maps.LatLng(" & tLat,tLong & ");" into line 16 of theData
      put theData into URL ("file:" & thehtmlFile)
      put "file://" & specialFolderPath("documents") & "/lcNav.html" into theURL
      replace space with "%20" in theURL
      iphoneControlSet browserID, "url", theURL
   else
      answer "Old Latitude: " & tOldLocation["latitude"] & return & "New Latitude: " & tNewLocation["latitude"] & return & "Old Longitude: " & tOldLocation["longitude"] & return & "New Longitude: " & tNewLocation["longitude"]
   end if
end locationChanged
Up above, in my preOpenCard section, I set 2 variables; tLocation and tOldLocation to the output of iphoneCurrentLocation like so:

Code: Select all

on preOpenCard
   iphoneStartTrackingLocation
   put iphoneCurrentLocation() into tLocation
   put iphoneCurrentLocation() into tOldLocation -- Recording current location twice so I can check against these values with the "on locationChanged" message later.
   if tLocation["latitude"] is not 0 or tLocation["latitude"] is not null then
      put tLocation["latitude"] into tLat
      put tLocation["longitude"] into tLong
   end if
etc, etc, etc.
What happens is my answer dialog appears and shows the exact same values for the new and old latitude/longitude. if I click "OK", it just comes back again over and over with the same values. So, unless I've messed up my logic here, the location really isn't changing. I believe this may be because the locationChanged message is actually calling some kind of iOS polling mechanism which just checks on the current location every so often (appears to be close to, if not a 1 second interval). It doesn't seem to be only getting called when the location has really changed. It's either that or locationChanged is also somehow checking for accelerometer or compass data too and THAT is what's changing. Seems like I'm going to have to write my own custom message/function to work around it at this point.

If you have any ideas, they would be greatly appreciated. You've been a big help already. I went ahead and attached a screenshot to show what I'm talking about.

Thanks Again!
Attachments
latlong.png

FireWorx
Posts: 362
Joined: Wed Sep 07, 2011 9:39 pm

Re: iOS, Google Maps and GPS, Oh My!

Post by FireWorx » Tue Dec 13, 2011 8:43 pm

Hi,
I am altering the HTML page with lat n Long coordinates just as you are prior to displaying the map page. I ran into the same issue with the delay when trying to obtain lat long coords in the same script that I started tracking the location. I played around with sending a mouse down message to another button, wait so many seconds, etc. What I ended up doing is start tracking location on the open card command and then stop location tracking in the close card. The google map interface is exclusive to that one card.

I am more interested in displaying directions and auto populating the "To" field so the user doesn't have to type much rather than real time GPS tracking. Although I find what you are doing to be very interesting. If you solve the problem please post the solution.

Also if you find a way to store kml files local on your iPad and display them from disc I would be interested in that. So far I can only find a way to pull them off of a web server and overlay them over the google map.

Dave

FireWorx
Posts: 362
Joined: Wed Sep 07, 2011 9:39 pm

Re: iOS, Google Maps and GPS, Oh My!

Post by FireWorx » Thu Dec 29, 2011 8:40 pm

Joseph,
Were you ever able to succesfully track your location over time and track it on a google map? I was thinking of experimenting around with tracking the location and heading of devise 1 and posting it to my MySQL web hosted database and then pulling that data down to a second IOS machine (devise 2)and comparing devise loc 1 to devise loc 2. Other coders... please feel free to chime in.
Dave

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: iOS, Google Maps and GPS, Oh My!

Post by Dixie » Thu Dec 29, 2011 10:55 pm

Joseph...

I haven't tried your code, I've just looked at it quickly... It;s still 'Christmas'...:-)

but why use round on tNewLocation ?... I do not think round is helping you here... all the numbers (up to 6 after the decimal point) make a difference to where the javaScript in the html file will think you are...

You also have a syntax error .. you wrote ( notice 'comma' not ',')

Code: Select all

put "var myLatlng = new google.maps.LatLng(" & tLat,tLong & ");" into line 16 of theData
this should be

Code: Select all

put "var myLatlng = new google.maps.LatLng(" & tLat & comma & tLong & ");" into line 16 of theData
be well

Dixie

tzenobite
Posts: 57
Joined: Sun Dec 04, 2011 3:59 pm

Re: iOS, Google Maps and GPS, Oh My!

Post by tzenobite » Sun Jan 01, 2012 10:31 pm

me too, i have a problem with getting gps coordinates
i simply need to get the position and i've tried many times with the 'standard' coding and the simulator
i ever get nothing from the iphonecurrentlocation function, no errors from iphonecantracklocation nor from iphonestarttrackinglocation
i've read here the timing issue so i've wrote this:

Code: Select all

on mouseup
iphoneStartTrackingLocation
   put "" into theloc
   put "0" into field "temp"
   repeat until theloc ≠ ""
   put "Ricerca..." into field "fgpsloc"
   wait for 1 second
   put iphonecurrentlocation() into theloc
   put field "temp" + 1 into field "temp"
end repeat
[...]
the rest of the script works well with some dumb coordinates
the script starts and run in circle, the number in the field grows up and the time run... and nothing happens
i tried this to see if the gps needs time to retrieve the gps data, but both in the simulator (which i think have some false coordinates just to fake the gps module) and the real iphone (jailbreaked) the script runs without any stop for many minutes while the map app in the iphone shows my position really fast, so isn't a gps module problem
i've the personal edition of lc ver. 5.0.0 and iphone 4 with ios 5.0.1
anyone wil help me? thanks!
how many hypercardist are needed to change a light bulb? one to actually do it while at least a dozen more are finding out a slightly different way to do it

proudly hypertalking since 1989

FireWorx
Posts: 362
Joined: Wed Sep 07, 2011 9:39 pm

Re: iOS, Google Maps and GPS, Oh My!

Post by FireWorx » Wed Jan 04, 2012 2:10 am

I start tracking location on the opencard handler and then stop tracking on the closecard handler. Once the user is on the card he or she clicks on a button to get and display the gps coordinates and they are always ready and display no prob. That works for me. If for some reason you want a look at my code just let me know.
Dave

tzenobite
Posts: 57
Joined: Sun Dec 04, 2011 3:59 pm

Re: iOS, Google Maps and GPS, Oh My!

Post by tzenobite » Wed Jan 04, 2012 7:55 am

FireWorx wrote:I start tracking location on the opencard handler and then stop tracking on the closecard handler. Once the user is on the card he or she clicks on a button to get and display the gps coordinates and they are always ready and display no prob. That works for me. If for some reason you want a look at my code just let me know.
Dave
yes thanks, so i can try to see where i'm wrong
how many hypercardist are needed to change a light bulb? one to actually do it while at least a dozen more are finding out a slightly different way to do it

proudly hypertalking since 1989

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Re: iOS, Google Maps and GPS, Oh My!

Post by BarrySumpter » Thu Apr 19, 2012 12:00 am

Hi all,

If anyone has solved this issue
I'd be very intersted in seeing the LC scripting results
and/or would we happy to assist by converting and testing on android.

tia
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

Post Reply