如何使用MapModule获取当前位置?

时间:2015-11-24 19:07:41

标签: javascript ios location titanium appcelerator

我正在尝试获取当前位置,但我不知道正确的代码,有人可以帮助我吗?这是我的代码:

var MapModule = require('ti.map');

var vc = MapModule.createAnnotation({

 title: 'You are Here',
 //subtitle: 'Sao Paulo, SP',
 pincolor: MapModule.ANNOTATION_GREEN,
 image:"/ui/images/you.png",

});

var walmart = MapModule.createAnnotation({

 latitude: -23.474891,
 longitude: -46.526756,
 title: 'Wallmart',
 subtitle: 'Guarulhos, SP',
 pincolor: MapModule.ANNOTATION_RED,
 image:"/ui/images/buscador_walmart.png",

}); 

var mapview = MapModule.createView({

top:0,
height: "80%",
mapType: MapModule.NORMAL_TYPE,
region: {latitude: win.latitude, longitude: win.longitude,
        latitudeDelta: 0.001, longitudeDelta: 0.001},
annotations: [vc,walmart],
animate:true,

});

mapview.visible = false;

win.add(mapview);

我还在 tiapp.xml 中添加了 NSLocationWhenInUseUsageDescription NSLocationAlwaysUsageDescription 来请求访问该位置。

有谁知道获取我当前位置的正确代码?

1 个答案:

答案 0 :(得分:1)

您可以这样做:

var onGetPositionError = function() {

    log('onGetPositionError');

    var lat = 52.3702157;
    var lon = 4.8951679;

    currentMapLat_ = lat;
    currentMapLon_ = lon;

    log('lat = ' +  lat + ' lon = ' + lon);

    $.mapView.setRegion({
        latitude: lat, longitude: lon,
        latitudeDelta: 0.005, longitudeDelta: 0.005
    });
};

var onGotPosition = function(e) {

    log('onGotPosition');

    var lon = e.coords.longitude;
    var lat = e.coords.latitude;

    currentMapLat_ = lat;
    currentMapLon_ = lon;

    log('lat = ' +  lat + ' lon = ' + lon);

    $.mapView.setRegion({
        latitude: lat, longitude: lon,
        latitudeDelta: 0.005, longitudeDelta: 0.005
    });
};

var getPosition = function() {
    Ti.Geolocation.getCurrentPosition(function(e) {
        if (e.error) {
            log(JSON.stringify(e));
            log('Cannot get current position');
            onGetPositionError();
            return;
        } else {
            onGotPosition(e);
            return;
        }
    });
};

$.locate.addEventListener('click', getPosition);