Titanium 获取当前地理位置(经纬度)Get the current location (latitude and longitude)

coffee 代码,下边的例子是用作实现签到功能的(需要js格式请去http://js2.coffee 上转换)

#=================  获取android 地理位置 ==================

    get_current_position_for_android = ->

        Ti.Geolocation.Android.manualMode = true

        network_provider =  Ti.Geolocation.Android.createLocationProvider

                name: Ti.Geolocation.PROVIDER_NETWORK,

                minUpdateDistance: 0.0,

                minUpdateTime: 1

        Ti.Geolocation.Android.addLocationProvider network_provider

        gps_provider =  Ti.Geolocation.Android.createLocationProvider

                name: Ti.Geolocation.PROVIDER_GPS,

                minUpdateDistance: 0.0,

                minUpdateTime: 1

        Ti.Geolocation.Android.addLocationProvider gps_provider

        gps_rule = Ti.Geolocation.Android.createLocationRule

            provider: Ti.Geolocation.PROVIDER_GPS,

            maxAge: 3000,

            accuracy: 100,

            minAge: 1000

        Ti.Geolocation.Android.addLocationRule gps_rule

        net_rule = Ti.Geolocation.Android.createLocationRule

            provider: Ti.Geolocation.PROVIDER_NETWORK,

            maxAge: 3000,

            accuracy: 100,

            minAge: 1000

        Ti.Geolocation.Android.addLocationRule net_rule

        #Ti.Geolocation.Android.manualMode = true

        location_callback = (e) ->

            if !e.success ||e.error

                show_alert "提示","签到失败,请重试!"

                return

            Ti.Geolocation.removeEventListener 'location',location_callback

            longitude = e.coords.longitude

            latitude = e.coords.latitude

            distance = getDistance(latitude, longitude, decorating_case_latitude, decorating_case_longitude)

            if distance < 1

                show_notice '距离超过1000米,不能签到'

                return

            post_longitude_and_latitude(longitude,latitude)



        Ti.Geolocation.addEventListener 'location',location_callback

        provider_network = null

        provider_gps = null

    #=================  获取ios 地理位置 ==================



    get_current_position_for_ios =  ->

        Titanium.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_BEST

        Titanium.Geolocation.getCurrentPosition (e) ->

            if (!e.success || e.error)

                show_alert("提示",'error ' + JSON.stringify(e.error))

            longitude = e.coords.longitude

            latitude = e.coords.latitude

            altitude = e.coords.altitude

            heading = e.coords.heading

            accuracy = e.coords.accuracy

            speed = e.coords.speed

            timestamp = e.coords.timestamp

            altitudeAccuracy = e.coords.altitudeAccuracy

            distance = getDistance(latitude, longitude, decorating_case_latitude, decorating_case_longitude)

            if distance > 1

                show_notice '距离超过1000米,不能签到'

                return

            post_longitude_and_latitude(longitude,latitude)

附带一个android bug


解决办法是 使 用 android 真 机 调 试!

ios 也需要真机。

你可能感兴趣的:(Titanium 获取当前地理位置(经纬度)Get the current location (latitude and longitude))