钛合金 - 如何在创建屏幕后运行程序

时间:2015-03-17 01:34:11

标签: google-maps titanium-alloy

我正在尝试更新地图视图中的当前位置。我得到控制器中的当前位置:

var updateCurrentLocation = function updateCurrentLocation(e){
Ti.API.info("Update current location on map");

$.map.setLocation({
    latitude: e.coords.latitude,
    longitude: e.coords.longitude,
    latitudeDelta: 1,
    longitudeDelta: 1
});
}

但问题是,此时代码运行,尚未创建地图视图,因此无法更新当前位置。 任何人都可以建议一些技术来解决这个问题吗? 谢谢!

3 个答案:

答案 0 :(得分:0)

您是不是在同一个控制器中创建地图?如果你只是将代码放在地图代码之后,但这很明显,所以我假设你已经想到了这一点。

创建地图时,为什么不将坐标设置为当前用户坐标?

最糟糕的情况是,如果要在500毫秒后调用updateCurrentLocation函数,可以使用setTimer(,timer ms)在设定的时间后调用函数。这不太理想。

答案 1 :(得分:0)

starting up the map controller

function showMap() {
    // create the new controller and pass in the
    // model object as an argument 'item'
    var ctrl = Alloy.createController('MapView', {
        'item' : args.item // <-- pass is information + coords for map
    });

    setTimeout(function() {
        args.photoListTab.open(ctrl.mainWindow);
    }, 200);
}

in map controller

// get the photo object from the parameters
var coords = args.item.custom_fields.coordinates[0];
var locationString = args.item.custom_fields.location_address;

// create annotation
var annotation = Alloy.Globals.Map.createAnnotation({
  latitude : Number(coords[1]),
  longitude : Number(coords[0]),
  title : args.item.custom_fields.location_string,
  myid : args.item.id
});

此处完整解决方案https://github.com/aaronksaunders/testInClass

答案 2 :(得分:0)

最终我发现Map View上有一个事件调用“完成”。因此,要在地图控制器中加载地图后执行任何操作,请使用:

$.map.addEventListener('complete', function(e) {
   Ti.API.info("Map controller: on Map complete");
   $.trigger('complete', e); // Trigger event for other controller can listen too.
   // And you can do other logic here.
});

来自Titanium Doc

的参考资料