Geolocation
Geolocation is the object that provides location information of the mobile device. Location information is measured through GPS (Global Positioning System) or WPS (Wi-Fi Positioning System), and it includes altitude, direction, latitude, longitude, and speed information.
Services based on location information have been established as major content indispensable on mobile. Especially, it is used in a variety of apps such as navigation, public transportation information, and delivery to find directions in combination with maps.
Obtaining Current Location Information
Location information can be obtained by calling the method of the Geolocation object. The getCurrentPosition method receives the location information of the current point. If you want to receive location information periodically, then use the watchStart method. When the method execution is successful, the onrecvsuccess event occurs, and values such as origin, altitude, direction, latitude, longitude, and speed can be obtained from the GeolocationEventInfo object.
${sample}
The following is an example of showing the location information of the mobile device by receiving location information from GPS or WPS. If you set the Accuracy and Interval Time and touch the Watch Start button, the location information is measured at the Interval Time intervals and displayed. If you touch the Watch Stop button, then the measure stops.
${sample_element}
- Geolocation > watchStart
This is the method that periodically receives the location information of the mobile device. Set accuracy and interval time as arguments. For accuracy, values between 0 to 2 in the case of Android, and values between 0 to 4 can be set. For interval time, it can be set between 200 and 86,400,000 in milliseconds.
- Geolocation > sourcetype
This is the property that has the source information that provided the location information. It has values of 1 for GPS and 2 for WPS. It cannot be used in the iOS environment.
- Geolocation > coords
This is the read-only property that has location information received from the mobile device. The location information of coords are as follows.
accuracy | The error range of location information |
altitude | Altitude information (in Meters) |
altitudeaccuracy | The error range of altitude information (available only in iOS) |
heading | Direction information (in degrees) |
latitude | Latitude information |
longitude | Longitude information |
speed | Speed information (in m/s) |
${sample_step}
1
Configuring the Screen
Add the Geolocation object. The added object can be checked in the Invisible Object window.
Place the Static component to display location information and the GoogleMap component appropriately as shown in the example figure.
Components and objects used to configure the screen are as follows.
Component / Object | ID |
---|---|
Static | Static00~Static11 |
Radio | rdo_accuracy |
Edit | edt_intervaltime |
edt_timestamp | |
edt_sourcetype | |
edt_accuracy | |
edt_altitude | |
edt_altitudeaccuracy | |
edt_heading | |
edt_latitude | |
edt_longitude | |
edt_speed | |
Button | btn_start |
btn_stop | |
GroupBox | GroupBox00 |
2
Writing the Watch Start Button Event Function
Write the onclick event function of the Watch Start button as follows.
this.btn_start_onclick = function(obj:nexacro.Button,e:nexacro.ClickEventInfo) { var nAccuracy = this.rdo_accuracy.text; var nIntervalTime = this.edt_intervaltime.value; if(nIntervalTime < 200 || nIntervalTime > 86400000) { this.edt_intervaltime.setFocus(); alert("Invalid range"); return; } this.Geolocation00.watchStart(nAccuracy, nIntervalTime); this.btn_start.set_enable(false); this.btn_stop.set_enable(true); };
3
Writing the Watch Stop Button Event Function
Write the onclick event function of the Watch Stop button as follows.
this.btn_stop_onclick = function(obj:nexacro.Button,e:nexacro.ClickEventInfo) { this.Geolocation00.watchStop(); this.btn_start.set_enable(true); this.btn_stop.set_enable(false); };
4
Writing the Geolocation Event Function
When the location information is successfully received, the onrecvsuccess event occurs and when it fails, the onrecverror event occurs.
When the location information is successfully received, the onrecvsuccess event occurs. You can obtain the sourcetype, accuracy, altitude, altitudeaccuracy, heading, latitude, longitude, and speed values from the GeolocationEventInfo object.
this.Geolocation00_onrecvsuccess = function(obj:nexacro.Geolocation,e:nexacro.GeolocationEventInfo) { this.edt_accuracy.set_value(e.coords.accuracy); this.edt_altitude.set_value(e.coords.altitude); this.edt_altitudeaccuracy.set_value(e.coords.altitudeaccuracy); this.edt_heading.set_value(e.coords.heading); this.edt_latitude.set_value(e.coords.latitude); this.edt_longitude.set_value(e.coords.longitude); this.edt_speed.set_value(e.coords.speed); var strSourceType = "UNKNOWN"; switch(e.sourcetype) { case "1": strSourceType = "GPS"; break; case "2": strSourceType = "WPS"; break; default: strSourceType = "UNKNOWN"; } this.edt_sourcetype.set_value(strSourceType); this.edt_timestamp.set_value(e.timestamp); };
When receiving the location information fails, the onrecverror event occurs. The error information is displayed as the pop-up for the reason as to why the error occurred.
this.Geolocation00_onrecverror = function(obj:nexacro.Geolocation,e:nexacro.GeolocationErrorEventInfo) { var strResult = "["+ e.errortype +" "+ e.statuscode +"] "+ e.errormsg; alert(strResult); };
5
Checking on the Mobile Device
Set accuracy and interval time, and touch the Watch Start button. If the location information is received normally, then the location information value in the Coords Information group box at the bottom is updated at each interval time. Touch the Watch Stop button to stop receiving location information.