如何在Sencha-Touch2.0中获取地图中当前位置的标记

时间:2012-04-26 12:08:18

标签: javascript sencha-touch extjs sencha-touch-2

嘿朋友我刚接触 Sencha Touch 平台。

我希望找到current location用户marker,请帮助我。我陷入了这种状况。

有没有新的好教程,请与我分享..

先谢谢你的时间。

Ravi Patel

3 个答案:

答案 0 :(得分:4)

好问题。我之前遇到过类似的问题。但是,后来,我找到了解决方案。

以下是解决问题的方法。

使用 Phonegap的地理位置API

进行演示
  1. 通过地理位置api获取地理位置。

    vat lat, lng;
    var geoLocationOptions = { maximumAge: 3000, timeout: 5000, enableHighAccuracy: true };
    navigator.geolocation.getCurrentPosition(geoLocationSuccess,geoLocationError,geoLocationOptions);
    
    function geoLocationSuccess(position) {
        lat = position.coords.latitude;
        lng = position.coords.longitude;
        Ext.Msg.alert("Geolocation","Latitude:"+ lat +", Longitude:"+ lng); 
    }
    
    function geoLocationError() {
       Ext.Msg.alert('Error','Error while getting geolocation');
    }
    
  2. 然后使用lat和lng的值将地图的中心设置为它,并将标记的位置设置为该值。

     ... 
     ...
     var position = new google.maps.LatLng(lat,lng);
     map = this.add({
            xtype: 'map',
            getLocation: true,
            autoLoad: true,
            mapOptions: {
                zoom: 15,
                center: position,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                navigationControl: true,
                zoomControl: true,
                mapTypeControl: true,
                scrollwheel: true,
                scaleControl: true,
                zoomControlOptions: {
                    style: google.maps.ZoomControlStyle.LARGE
                }
            }
        });
    
     marker = new google.maps.Marker({
            id: 'geoLocMarker',
            position: position,
            map: map.getMap(),
            visible: true
     });
     ...
     ...
    

答案 1 :(得分:0)

如果您只使用Ext.Map.getGeo(),这会更简单:

    var geo = extmap.down("#Map").getGeo();
    var currentPosition = new google.maps.LatLng(geo.getLatitude(), geo.getLongitude());

只要您实例化地图,如果浏览器客户端允许您获取该地图,它应该获取当前位置。

HTH!

答案 2 :(得分:0)

我也使用sencha touch 2.3.1为simillar app工作 使用Ext.util.Geolocation类获取当前位置,并使用以下代码自动更新currentPosition。

Ext.create('Ext.util.Geolocation', {
autoUpdate: true,
allowHighAccuracy: true,
frequency: '5000', // set the milliseconds for the location update
listeners: {
locationupdate: function(geo) {
    latitude=Global.currentUserLocations.currentLat;
    longitude=Global.currentUserLocations.currentLong;
    if(Global.currentUserPositionMarker)
    {
        latlng1=new google.maps.LatLng(latitude, longitude);
        Global.currentUserPositionMarker.setPosition(latlng1);
    }
   }
 }
});  

上面的代码对我有用,我已经在我的应用程序中用于获取currentLocation并将标记移动到currentPosition。

相关问题