Adding Geolocation and Map view to your app
It is very easy to add full geo location and map view to an existing application.
Download Geo Location Sample- Add a geo and an address column to your existing table structure.
- When creating a new record on the mobile, get the current phone location and address using
- Add an icon in your app to display a map screen with the records as pinpoints, using the Map.addItem() and Map.show() methods
Add a 2 new columns to your existing table structure
- geo will contain the geo coordinates of your record. it is a string format "latitude,longitude" for example "1.2345,56,3442"
- address will contain human readable address corresponding to the geo coordinates, for example "234 Broadway, New York, USA"
... Config.tables["defects"] = "id;name;geo;address;owner;date DATE"; ....
Get the phone current location and address during new record creation
In the code to insert a new record,- use Settings.getLocation() to get the geo location of the phone
- and Settings.getAddress(geo) to obtain the address from the geo coordinates.
- Store these 2 information in the newly created record with Query.insert(table, values)
function newDefect() {
var values = { date: Date.now(), owner:User.getName(), name:"new defect" };
values.geo = Settings.getLocation();
values.address = Settings.getAddress(values.geo);
var id = Query.insert("defects", values);
History.redirect("editDefect({id})");
}
Display the record on a map, with clickable pins
use Map.addItem(label, geo, onclick) and Map.show() to display all records on a map with click links
function viewMap() {
var defects = Query.select("defects", "id;name;geo");
for (var i = 0; i < defects.length; i++) {
var defect = defects[i];
Map.addItem(defect.name, defect.geo, "viewDefect({defect.id})");
}
Map.show();
}
- Make sure your are returning the geo column in the Query.select() statement
- You cannot display both a map and a list at the same time.
- Do not add any other statement after Map.show()