Using Geolocation in a Worklight Application
DavidDhuyvetter 10000034CQ Comment (1) Visits (11544)
The Map Application that I've been developing here uses a fixed center point pretty much in the middle of nowhere. A more common pattern with mobile apps is to use the current location of the user as the center of the map. In a Worklight application, this is achieved using the HTML 5 Geolocation API.
Modify the GoogleMapWidget to use geolocation
The Geolocation API is asynchronous, so the real work of setting the map's center is in
The callback function receives a
Only latitude, longitude and accuracy are required to be set by the runtime. the accuracy attribute indicates the accuracy of the latitude and longitude coordinates, and is specified in meters.
Test on Chrome and iPhone emulator
With these changes, the GoogleMap app will now use the current location to center the map. If you run the application in chrome, the browser will now ask permission to use the loaction when you start the app
When you Allow the app to track the current location, the map will open to what is usally a reasonable approximation of the current location. There is a write up of how Chrome determines the current location here.
If you run the application on the iPhone simulator, that too will ask permission to use the current location.
When you click on OK, the app will open to the simulator's location. I've seen different results for the current location in the iPhone simulator. Sometimes it thinks it is in Cupertino, California, and sometimes it seems to have a reasonably good approximate current location. I'm not sure why it sometimes has a real location and sometimes a default location.
Test on Android Emulator
If you run the app on the Android emulator, however, there are a number of problems.
The first problem is that you need to specify in the AndroidManifest.xml file that you will need permission to access the location services. AndroidManifest.xml is located in the root directory of the generated Android project that is associated with the Worklight app. It has a GUI editor , or you can edit it as an XML file. You need to add the following permissions:
The next problem is that by default the Google Location Service is disabled on the emulator. You need to go to Settings -> Location Services on Android, and ensure that Google's Location Services are enabled.
With these two changes the app will work on an actual Android device, but it will still fail on the Android emulator. This is because Coarse Location (Network Based) just doesn't work on the Android emulator.
Use high accuracy location
The fact that Coarse Location doesn't work in the Android emulator is probably OK, because for most map apps you are going to want to work with the Fine Location (GPS Based) anyway.
To use the Fine Location, update the call to
There are other values (such as a timeout for the call) that you can set in the positionOptions parameter, but for now, we will just use it to enable high accuracy. This option is only a hint, so the device is free to ignore it. Enabling high accuracy may result in significantly greater battery usage and a much slower response, as getting an initial GPS fix can take several seconds even in the best case.
Now if you run the App on an Android device, you may notice that the GPS icon appears briefly in the status bar as the app launches. If the GPS icon does not appear as the app launches, check Settings -> Location Services and make sure that Standalone GPS Services are enabled.
In the Android Emulator, the app will start. There will be no geolocation failure, but
To give the emulator a position, in Eclipse, switch to the DDMS perspective. In the Devices view, select the running emulator. In the Emulator Control view, Scroll down to the Location controls, enter the desired Longitude and Latitude, and click on Send.
As soon as you click on Send in the DDMS perspective,
There is lots more that can be done with location services. The API allows the caller to specify a maximum age for the location, and as noted, the position in the callback includes an indication of the accuracy of the result.
The updated sample app can be downloaded here. I am not including the Android environment in the zip file (because of size) so you if you want to use the app on android you will have to edit the AndroidManifest.xml to add the required permissions after you add the environment