Page 1 of 1

Add a marker at a clicked point

Posted: July 31st, 2024, 8:18 am
by mussabahire
Hello Cspro users.

With markerId = mymap.addMarker(38.84839, -76.931098) we can get the marker on a certain points.
How about when I want to click on current location, and get the points saved in my application and mark it?
For instance, I have some tpk files, I want to click on a household(a point) and get the coordinates of that point.
You can also help me load some tpk files, click on it and open an external application.

Regards,

Mussa.

Re: Add a marker at a clicked point

Posted: July 31st, 2024, 10:07 am
by justinlakier
Hello,

You can call Map.setOnClick() to define a behavior for clicking the map. This should then use a callback function, which takes Map.getLastClickLattitude() and Map.getLastClickLongitude() to get the location of the click, then uses them for Map.addMarker() at that click's location.

For your other question about opening tpk files in another application, you can use Path.selectFile() to allow the user to select a specific tpk file in your map data folder. Google limits the opening of other apps by CSEntry on Android, however this page provides some details on how to use SystemApp to launch other applications, as well as how to share files or call into CSEntry from other applications with the Action Invoker.

Hope this helps,
Justin

Re: Add a marker at a clicked point

Posted: July 31st, 2024, 10:34 am
by mussabahire
// Add a marker to the map at latitude 38.84839, longitude -76.931098
markerId = mymap.addMarker(38.84839, -76.931098);
This will add a marker to a certain point.
How to make the marker go to your current position and take the point there?
Let's say you have loaded a tpk image and you move the pointer to a Household. How to record the latitude and longitude of that clicked point?

Re: Add a marker at a clicked point

Posted: July 31st, 2024, 1:48 pm
by justinlakier
Hello,

As I mentioned, you need to use Map.setOnClick(). Go to the documentation page for this function. What it does is set a specific callback function to be called whenever you click on a position on the map that is not already a marker. You will need to create this callback function. Within this callback function, you can use Map.getLastClickLatitude() and Map.getLastClickLongitude() to get the position of the click. The example callback function given in the documentation for Map.setOnClick() uses Map.setMarkerLocation() to move an existing marker with an existing ID to those coordinates. If you want to create a new marker instead, then you should use Map.addMarker(). I have put a modification of the example below. Note that Map.setOnClick() can't be called in the GLOBAL proc.
PROC GLOBAL
Map
mymap;

// This function is called when user taps on map.
// It adds a marker to the location that the user tapped.
function mapClicked()
    numeric lat = mymap.getLastClickLatitude();
    numeric lon = mymap.getLastClickLongitude();
    mymap.addMarker(lat, lon);
end;

PROC SHOW_MAP
preproc

// Set function that is called when the user taps on the map to add the marker
// to the new position
mymap.setOnClick(mapClicked());
Hope this helps,
Justin