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

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.

主站蜘蛛池模板: 洱源县| 青州市| 彭阳县| 准格尔旗| 新绛县| 淮阳县| 厦门市| 阳信县| 舞钢市| 蓬莱市| 河南省| 大兴区| 久治县| 盐亭县| 洛阳市| 婺源县| 休宁县| 登封市| 美姑县| 商都县| 南江县| 金华市| 合作市| 贺兰县| 达拉特旗| 浦县| 福州市| 靖边县| 长汀县| 西乡县| 左权县| 内黄县| 监利县| 会理县| 新津县| 崇信县| 西和县| 商洛市| 图片| 常熟市| 民权县|