【问题标题】:Titanium Mapview Click Get the Lat and LongTitanium Mapview 单击获取经度和经度
【发布时间】:2012-08-14 14:59:55
【问题描述】:

我希望在 Titanium (appcelerator) 中创建一个简单的地图视图,但我想要/需要做的是让用户单击或长按地图并从地图上的该选择中获取纬度和经度。地图视图确实有一个mapview.addEventListener('click',function(evt)),但单击时我没有得到这些值。对此的任何帮助都会很棒!

【问题讨论】:

    标签: titanium android-mapview appcelerator


    【解决方案1】:

    从点击或长按坐标计算纬度和经度非常容易(如果不是太缩小的话)。在取自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
    
        };
    }
    

    请注意,您无法在 MapView 本身上侦听 longpress 事件,因此将其嵌套在常规容器视图中并向其中添加侦听器。示例用法如下:

    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;
    });
    

    如果您查看地球的一个(相对)小区域,当您完全缩小时,由于地球的曲率,您必须使用墨卡托投影来获得真实坐标,如果您想完全缩小然后使用该模块。

    【讨论】:

    • 看起来 mapview.addEventListener('longpress') 或 mapview.addEventListener('click') 没有在钛合金中触发。我试过这两个事件都没有被触发或返回值
    • 将地图视图添加到容器视图中,然后将侦听器添加到容器视图中,它将完全一样。
    • 这似乎是这样做的,如果其他人遇到这些类型的问题,请确保在加载地图时为地图设置/预定义一个区域。如果您不这样做,则地图区域将未定义,从而导致错误。谢谢约西亚
    【解决方案2】:

    市场上有一个模块可以做到这一点。购买价格为 0.99 美元,并会为您提供地图坐标https://marketplace.appcelerator.com/apps/1334

    【讨论】:

    • 我看到了这个,但它只有 IOS。不过感谢您的查找。
    猜你喜欢
    • 2015-07-16
    • 2013-01-07
    • 2019-03-25
    • 2016-09-29
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    相关资源
    最近更新 更多