官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 璧山县| 从江县| 霍林郭勒市| 清原| 都匀市| 朝阳区| 荆州市| 闵行区| 苗栗市| 龙川县| 木兰县| 新兴县| 白银市| 水城县| 乌拉特中旗| 抚州市| 开江县| 锡林郭勒盟| 凉城县| 德清县| 固镇县| 虞城县| 武强县| 屏边| 册亨县| 荔浦县| 弥渡县| 大庆市| 普兰县| 文昌市| 贵州省| 桐梓县| 庐江县| 施甸县| 清镇市| 平昌县| 安陆市| 普兰店市| 陇川县| 永年县| 昌平区|