Getting coordinates from the webmap at your mouse position
6 views (last 30 days)
Show older comments
Hello,
I want to create a map where the user can set a pin and i also want to save the coordinates of the pin afterwards. I tried to work with the webmap and also with geoplot, but both ways dont really work well. A solution would be very appreciated.
Thank you for your help.
Greetings
Daniel
0 Comments
Answers (1)
Yash
on 21 Jun 2023
You can use the mapping toolbox in MATLAB. I think the geoshow and save function may help (for display and saving). Refer to this code for some more understanding.
%setting up a figure with a Mercator projection for the map
figure
ax = axesm('MapProjection', 'mercator');
set(ax, 'Units', 'normalized', 'Position', [0 0 1 1]);
%set a pin given by user
[lat, lon] = inputm(1);
%display the pin
geoshow(lat, lon, 'Marker', 'o', 'Color', 'red', 'MarkerSize', 10);
%save the pin
save('pin_coordinates.mat', 'lat', 'lon');
%end of code
Hope this helps.
3 Comments
Yash
on 22 Jun 2023
For this, you can integrate the Leaflet library for MATLAB with the leaflet function and Leaflet's JavaScript capabilities. To start, download and install the Leaflet library for MATLAB from the MATLAB File Exchange and add the library to your MATLAB path. Then download the Leaflet JavaScript library from the official Leaflet website: Leaflet.js and place the Leaflet JavaScript files (leaflet.js and leaflet.css) in the same directory as your MATLAB script. Then run the following MATLAB code.
%Creating map with zoom functionality
map = leaflet('MapCenter', [0, 0], 'ZoomLevel', 2);
%Set pin interactively
marker = addMarker(map, 'Draggable', true);
%Save the coordinates
lat = marker.Lat;
lon = marker.Lon;
save('pin_coordinates.mat', 'lat', 'lon');
%Display the map
map.Visible = true;
Make sure that the Leaflet JavaScript files are properly included in your MATLAB script or project folder for the integration to work correctly.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!