Issue
I am looking to create a simple mapview in Titanium (appcelerator), but what I want/need to do is have the user click, or longpress on the the map and get the latitude and longitude from that selection on the map. The mapview does have a mapview.addEventListener('click',function(evt))
, but I dont get a these values when clicked. Any help with this would be awesome!
Solution
It is very easy to just calculate the latitude and longitude from the click or longpress coordinates (if were not too zoomed out). Use this function inside the event listener taken from this question at Appcelerator Q&A:
var calculateLatLngfromPixels = function(mapview, xPixels, yPixels) {
var region = mapview.actualRegion || mapview.region;
var widthInPixels = mapview.rect.width;
var heightInPixels = mapview.rect.height;
// should invert because of the pixel reference frame
heightDegPerPixel = -region.latitudeDelta / heightInPixels;
widthDegPerPixel = region.longitudeDelta / widthInPixels;
return {
lat : (yPixels - heightInPixels / 2) * heightDegPerPixel + region.latitude,
lon : (xPixels - widthInPixels / 2) * widthDegPerPixel + region.longitude
};
}
Note that you cant listen for a longpress
event on the MapView itself so nest it in a regular container view and add the listeners to that. Example usage like this:
var container = Ti.UI.createView({
top : 0,
left : 0
});
container.add(mapview);
container.addEventListener('longpress', function(e) {
var coordinate = calculateLatLngfromPixels(mapview, e.x, e.y);
var longitude = coordinate.lat;
var latitude = coordinate.lat;
});
This will work if your looking at a (relatively) small area of the earth, when youre fully zoomed out, you would have to use a mercator projection to get real coordinates because of the curvature of the earth, if you want to be fully zoomed out then use the module.
Answered By - Josiah Hester
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.