- Appcelerator Titanium Smartphone App Development Cookbook(Second Edition)
- Jason Kneen
- 486字
- 2021-07-30 10:09:38
Getting your current position using GeoLocation
Our map may be working, but it is currently hardcoded to appear above London, England, and not all of us live and work in that big city. One of the great things about mapping technology is that we can determine our location from anywhere in the world via GPS satellites, Wi-Fi networks, and cellphone towers. This allows you to put maps into context, and lets you issue data to your user that is targeted to their physical location.
In order to get our current location, we need to use the Ti.Geolocation
namespace, which contains a method called getCurrentPosition
. The next recipe will explain how to use this namespace to adjust the bounds of the MapView
to your current location.
The complete source code for this recipe can be found in the /Chapter 3/Recipe 2
folder.
How to do it...
Add in the following code after you have added your MapView
component to the window:
//apple now requires this parameter so it can inform the user //of why you are accessing their location data Ti.Geolocation.getCurrentPosition(function(e) { if (e.error) { //if mapping location doesn't work, show an alert alert('Sorry, but it seems geo location is not available on your device!'); return; } //get the properties from Ti.GeoLocation var longitude = e.coords.longitude; var latitude = e.coords.latitude; var altitude = e.coords.altitude; var heading = e.coords.heading; var accuracy = e.coords.accuracy; var speed = e.coords.speed; var timestamp = e.coords.timestamp; var altitudeAccuracy = e.coords.altitudeAccuracy; //apply the lat and lon properties to our mapview mapview.region = {latitude: latitude, longitude: longitude, latitudeDelta:0.5, longitudeDelta:0.5 }; });
Run your app in the simulator and you should have a screen appear that looks just like the following:
Note
Note that on the simulator, unlike a real device, you can change your location by selecting the Debug | Location menu and setting it to a longitude and latitude of your choice. You can also select from some predefined location types, such as Freeway drive and City Run. These are useful for testing code that tracks a changing location.

How it works…
Getting our current position is simply a matter of calling the getCurrentPosition
method of the Ti.Geolocation
namespace and capturing the properties returned when this event fires. All of the information that we need is then accessible via the coords property of the event object. In the preceding example source code, we set a number of these properties to variables, some of which we will use in our Exercise Tracker application later on. Finally, we took the latitude and longitude properties from the coords object and reset the MapView's region according to these new values.
Here's an important note for iOS applications: as of iOS 8, you need to add the following to your TiApp.xml
file:
<ios> <plist> <dict> <key>NSLocationWhenInUseUsageDescription</key> <string>To obtain user location for tracking distance travelled</string> </dict> </plist> </ios>
This will tell the user why you are using location services, and it is now a requirement.
- 虛擬儀器設(shè)計(jì)測(cè)控應(yīng)用典型實(shí)例
- PPT,要你好看
- 機(jī)器學(xué)習(xí)及應(yīng)用(在線實(shí)驗(yàn)+在線自測(cè))
- Hands-On Artificial Intelligence on Amazon Web Services
- R Data Mining
- 教父母學(xué)會(huì)上網(wǎng)
- 自主研拋機(jī)器人技術(shù)
- 機(jī)器學(xué)習(xí)流水線實(shí)戰(zhàn)
- Machine Learning Algorithms(Second Edition)
- ASP.NET 2.0 Web開(kāi)發(fā)入門(mén)指南
- Linux系統(tǒng)下C程序開(kāi)發(fā)詳解
- Hands-On SAS for Data Analysis
- Web滲透技術(shù)及實(shí)戰(zhàn)案例解析
- 圖像傳感器應(yīng)用技術(shù)
- C# 2.0實(shí)例自學(xué)手冊(cè)