Total Pageviews

Saturday, May 24, 2014

Event Location in detail (1)

Event Location (GPS)
សំរាប់ location events បានបង្កើតឡើងដោយ GPS hardware វានឹងបញ្ចូនទិន្នន័យទៅកាន់ global Runtime object។ ចំណាំះ នៅក្នុង event listener event.errorCode ត្រូវបាន check នៅពេលដែល location error។ វាត្រូវបាន disabled នៅលើ Location Services សំរាប់ iOS ចំណែកនៅក្នុង GPS on android devices ត្រូវដាក់ permission ដល់វា។
កូដសំរាប់ដាក់ permission នៅក្នុង build.settings:
settings =
{
    android =
    {
        usesPermissions =
        {
            -- Permission to retrieve current location from the GPS.
            "android.permission.ACCESS_FINE_LOCATION",

            -- Permission to retrieve current location from WiFi or cellular service.
            "android.permission.ACCESS_COARSE_LOCATION",
        },
    },
}
នេះជា Properties របស់វា
event.direction
សំរាប់ direction the device is travelling in degrees clockwise ចេញពី true North បើសិនជា negative the direction is invalid
event.accuracy
វាផ្តល់នៅភាពត្រឹមត្រូវរបស់ GPS location in meters if negative then the latitude and longitude are not valid
event.errorCode
សំរាប់ Platform specific integer សំរាប់ error condition។ សំរាប់ property only exits នៅពេលដែល GPS error កើតឡើង បើមិនទេវានឹងជា nil
event.altitude
វាផ្តល់នៅ altitude in meters នៃ current GPS location
event.name
វាជា String location
សូមមើលកូដះ
local function locationHandler( event )
    print( "Location name: " .. event.name )
    print( "Location latitude: " .. event.latitude )
    print( "Location longitude: " .. event.longitude )
end

Runtime:addEventListener( "location", locationHandler )

event.time
សំរាប់ event time នឹងត្រឡប់ UTC timestamp នៃ​GPS location event
event.longitude
សំរាប់ longitude នៅក្នុង degrees របស់ current GPS location
សូមមើលកូដះ
local locationHandler = function( event )
    -- Check for error (user may have turned off Location Services)
    if event.errorCode then
        native.showAlert( "GPS Location Error", event.errorMessage, {"OK"} )
        print( "Location error: " .. tostring( event.errorMessage ) )
    else
        local latitudeText = string.format( '%.4f', event.latitude )
        currentLatitude = latitudeText
        latitude:setText( latitudeText )

        local longitudeText = string.format( '%.4f', event.longitude )
        currentLongitude = longitudeText
        longitude:setText( longitudeText )

        local altitudeText = string.format( '%.3f', event.altitude )
        altitude:setText( altitudeText )

        local accuracyText = string.format( '%.3f', event.accuracy )
        accuracy:setText( accuracyText )

        local speedText = string.format( '%.3f', event.speed )
        speed:setText( speedText )

        local directionText = string.format( '%.3f', event.direction )
        direction:setText( directionText )

        -- Note: event.time is a Unix-style timestamp, expressed in seconds since Jan. 1, 1970
        local timeText = string.format( '%.0f', event.time )
        time:setText( timeText )
    end
end

-- Activate location listener
Runtime:addEventListener( "location", locationHandler )

event.errorMessage
សំរាប់ String ដែលពណ័នានៅ error in acquiring a GPS location ចេញពី device បើសិនជា error កើតឡើងនៅក្នុង Property ជា nil ពេលនោះ String របស់វាអាចជា localizing according to the user’s language


No comments:

Post a Comment