- 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.
- Deep Learning Quick Reference
- 樂高機(jī)器人EV3設(shè)計指南:創(chuàng)造者的搭建邏輯
- TIBCO Spotfire:A Comprehensive Primer(Second Edition)
- 自動檢測與轉(zhuǎn)換技術(shù)
- 最簡數(shù)據(jù)挖掘
- 工業(yè)機(jī)器人現(xiàn)場編程(FANUC)
- 完全掌握AutoCAD 2008中文版:綜合篇
- 大學(xué)C/C++語言程序設(shè)計基礎(chǔ)
- Linux服務(wù)與安全管理
- 網(wǎng)絡(luò)服務(wù)搭建、配置與管理大全(Linux版)
- 基于神經(jīng)網(wǎng)絡(luò)的監(jiān)督和半監(jiān)督學(xué)習(xí)方法與遙感圖像智能解譯
- Building a BeagleBone Black Super Cluster
- INSTANT VMware vCloud Starter
- Hands-On Dashboard Development with QlikView
- Spark大數(shù)據(jù)商業(yè)實戰(zhàn)三部曲:內(nèi)核解密|商業(yè)案例|性能調(diào)優(yōu)