Using Marker to Spot a particular address in a Map (Sencha Touch 2) Sencha Map

非 Y 不嫁゛ 提交于 2019-12-04 18:50:40

You're on your own for directions but he're dropping the pins (Assuming you add lat/long to your model and pass the model to the view when its created):

Ext.define('List.view.PresidentDetail', {
    extend : 'Ext.Map',
    xtype : 'presidentdetail',
    config : {
        title : 'Details',
        styleHtmlContent : true,
        scrollable : 'vertical',
        president: null
    },
    onGeoUpdate : function(geo) {
        if (geo) {
            this.fireEvent('setcenter', this, this.getMap(), geo);
        }
    }
});

And in your controller:

Ext.define('List.controller.PresidentDetailController', {
    extend : 'Ext.app.Controller',
    config : {
        control : {
            'presidentdetail' : {
                activate : 'activateGPS',
                setcenter : 'dropPins'
            }
        },
        markersArray : [],
    },
    activateGPS : function(container, options){
        container.setUseCurrentLocation(true);
    },
    dropPins : function(component, map, geo, eOpts) {
        //only do this the first time? 
        //geo updates are constantly recieved so turn useCurrentLocation off
        comp.setUseCurrentLocation(true);

        //Remove all markers from the map.
        for(var i = 0; i < this.getMarkersArray().length; i++) {
            this.getMarkersArray()[i].setMap(null);
        }
        this.setMarkersArray(Array());

        var currentPosition = new google.maps.LatLng(geo.getLatitude(), geo.getLongitude());
        var yourLocationMarker = new google.maps.Marker({
                position : currentPosition,
                title : 'Current Location',
                map : map
            });
        self.getMarkersArray().push(yourLocationMarker);

        var president = comp.getPresident();
        var presidentsPosition = new google.maps.LatLng(president.getLatitude(), president.getLongitude());
        var yourLocationMarker = new google.maps.Marker({
                position : presidentsPosition,
                title : president.fullName(),
                map : map
            });
        self.getMarkersArray().push(presidentsPosition);
    }
});

EDIT Here is the model with the latitude and longitude fields added:

Ext.define('List.model.President', {
    extend : 'Ext.data.Model',
    config : {
        fields : ['firstName', 'middleInitial', 'lastName', 'latitude', 'longitude']
    },
    fullName : function() {
        var d = this.data, names = [d.firstName, (!d.middleInitial ? "" : d.middleInitial + "."), d.lastName];
        return names.join(" ");
    }
});

And the store with the additional fields:

Ext.define('List.store.Presidents', {
    extend : 'Ext.data.Store',
    config : {
        model : 'List.model.President',
        sorters : 'lastName',
        grouper : function(record) {
            return record.get('lastName')[0];
        },
        data : [{
            firstName : "PEJABAT WILAYAH SARAWAK",
            lastName : "Ground, 1st & 3rd Floor, Section 6, Kuching Town Land District (KTLD), Jalan Kulas, 93400 Kuching, Sarawak",
            latitude : 4.600381 ,
            longitude : 101.094174
        }, {
            firstName : "PEJABAT WILAYAH TERENGGANU",
            lastName : "Lot PT 3593, Ground, Mezzanine & 1st Floor, Jalan Sultan Zainal Abidin, 20000 Kuala Terengganu, Terengganu",
            latitude : 5.336649,
            longitude : 103.142497
        }, {
            firstName : "PEJABAT WILAYAH MELAKA",
            lastName : "No. 10, Jalan Melaka Raya 8, Taman Melaka Raya, 75000, Melaka. ",
            latitude : 2.184044,
            longitude : 102.25982
        }]
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!