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

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.

主站蜘蛛池模板: 中江县| 稻城县| 西宁市| 奉贤区| 建昌县| 岳阳县| 德庆县| 郁南县| 西充县| 大同市| 滕州市| 大邑县| 门源| 新密市| 苏尼特右旗| 临安市| 阿巴嘎旗| 三都| 万州区| 方正县| 合阳县| 资源县| 普兰县| 通许县| 龙里县| 古田县| 花莲县| 高密市| 建始县| 府谷县| 铜梁县| 景洪市| 博乐市| 延寿县| 罗山县| 田阳县| 金溪县| 靖江市| 阿巴嘎旗| 达州市| 定州市|