简体   繁体   中英

How to get Current location using MapModule?

I'm trying to get my current location but I don't know the right code, can anyone help me? This is my code below:

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

I also put NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription in tiapp.xml to request access the location.

anyone knows the right code to get my current location?

You can do something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM