最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How Can I Get marker on current location in map in Sencha-Touch2.0 - Stack Overflow

programmeradmin3浏览0评论

Hey friends i m new to Sencha Touch platform.

And I want to find user current location with marker please help me out. i stuck in this condition.

Is there any new good tutorial for me then please share with me..

Thanks in Advance for your time.

Ravi Patel

Hey friends i m new to Sencha Touch platform.

And I want to find user current location with marker please help me out. i stuck in this condition.

Is there any new good tutorial for me then please share with me..

Thanks in Advance for your time.

Ravi Patel

Share Improve this question asked Apr 26, 2012 at 12:08 Ravi PatelRavi Patel 632 silver badges5 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 4

Nice question. I faced a similar problem before. But, later, I figured out the solution.

Here's how you can solve it.

  • Either use Phonegap's Geolocation API or ..
  • Use Ext.util.Geolocation from Sencha Touch API

Demo with Phonegap's Geolocation API

  1. Get the geolocation via the geolocation 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. Then use the values of lat and lng to set the map's center to it as well as set the marker's position to that value.

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

It's a lot simpler if you just use Ext.Map.getGeo() as follows:

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

As long as you have your map instantiated, it should get the current location if the browser client allows for you to obtain that.

HTH!

I have also work for simillar app using sencha touch 2.3.1
Using Ext.util.Geolocation class for get current location and automatically update the currentPosition using the following code.

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

The above code worked for me and i have already used in my app for getting currentLocation and moved the marker into currentPosition.

发布评论

评论列表(0)

  1. 暂无评论