Skip to content

Commit

Permalink
create markers form response for later use
Browse files Browse the repository at this point in the history
  • Loading branch information
britiger committed Feb 3, 2020
1 parent b103419 commit 77c9206
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
28 changes: 22 additions & 6 deletions CriticalMapsAPIBarrel.mc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ using Toybox.Cryptography;
using Toybox.Communications;
using Toybox.Math;
using Toybox.Lang;
using Toybox.StringUtil;
using Toybox.StringUtil;
using Toybox.WatchUi;

(:background)
module CriticalMapsAPIBarrel {
Expand All @@ -21,6 +22,7 @@ module CriticalMapsAPIBarrel {
var countCM10 = 0;
var chatText = "";
var deviceIdHash = "";
var mapMarkers = [];

function getDeviceId() {
if (!deviceIdHash.equals("")) {
Expand Down Expand Up @@ -59,9 +61,9 @@ module CriticalMapsAPIBarrel {
propDeviceId = "";
}
if (propDeviceId == null || propDeviceId.equals("")) {
// need initalisation
// call in non-background for inital set value
// setValue is not possible in background
// initalisation of application property
// need to call in non-background for inital set value
// setValue is not possible in background!
var mySettings = System.getDeviceSettings();
propDeviceId = mySettings.uniqueIdentifier;
try {
Expand All @@ -76,7 +78,8 @@ module CriticalMapsAPIBarrel {
function sendPositionData(callbackMethod) {
var url = BASEURL + "postv2";

var positionInfo = Position.getInfo();
var positionInfo = Position.getInfo(); // get current Postion
// Check position
if (positionInfo.accuracy < Position.QUALITY_POOR) {
// Location not good enough
return -1; // use last position
Expand All @@ -86,14 +89,17 @@ module CriticalMapsAPIBarrel {
System.println("Longitude: " + myLocation[1]);
System.println("Accu: " + positionInfo.accuracy);

// Check Position can be real or it's only a dummy
if (myLocation[0] >= 179 || (myLocation[0] == 0 && myLocation[1] == 0)){
return -2;
}
var location = {
"latitude" => myLocation[0]*1000000,
"longitude" => myLocation[1]*1000000
};
// save valid location
lastLocation = location;
// setting parameters for sending to critical maps api
var parms = {
"location" => location,
"device" => getDeviceId()
Expand All @@ -113,14 +119,15 @@ module CriticalMapsAPIBarrel {
function callbackCM(responseCode, data) {
System.println("Response: " + responseCode);
lastResponse = responseCode;
mapMarkers = [];
if (responseCode == 200) {
System.println("Data: " + data);
// Check Structure of response
if(data instanceof Toybox.Lang.Dictionary && data.hasKey("locations")) {
parseData(data);
}
}
return {"responseCode" => responseCode, "nearestCM" => nearestCM, "countCM10" => countCM10, "chatText" => chatText};
return {"responseCode" => responseCode, "nearestCM" => nearestCM, "countCM10" => countCM10, "chatText" => chatText, "mapMarkers" => mapMarkers};
}

function parseData(data) {
Expand All @@ -136,6 +143,15 @@ module CriticalMapsAPIBarrel {
if (dist < 10) { // TODO: Should be configurable
count10 += 1;
}
// Add location to list of marker
mapMarkers.add(new WatchUi.MapMarker(
new Position.Location({
:latitude => entry["latitude"]/1000000.0,
:longitude => entry["longitude"]/1000000.0,
:format => :degrees
})
)
);
}
nearestCM = nearest;
countCM10 = count10;
Expand Down
2 changes: 1 addition & 1 deletion manifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- This is a generated file. It is highly recommended that you DO NOT edit this file. --><iq:manifest xmlns:iq="http://www.garmin.com/xml/connectiq" version="3">
<iq:barrel id="3f84178eba974605b31604eacfcf94e4" minSdkVersion="2.4.0" module="CriticalMapsAPIBarrel" version="0.2.0">
<iq:barrel id="3f84178eba974605b31604eacfcf94e4" minSdkVersion="2.4.0" module="CriticalMapsAPIBarrel" version="0.3.0">
<iq:products>
<iq:product id="edge1030"/>
<iq:product id="edge1030bontrager"/>
Expand Down

0 comments on commit 77c9206

Please sign in to comment.