现在的位置: 首页 > 综合 > 正文

sencha touch权威指南—学习笔记5-经纬度获取计算直线距离

2018年01月29日 ⁄ 综合 ⁄ 共 2788字 ⁄ 字号 评论关闭

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <link rel="stylesheet" href="sdk-touch/resources/css/sencha-touch.css" />
    <link rel="stylesheet" href="resources/css/app.css" />
    <!--<script src="sdk-touch/sencha-touch.js"></script>-->
    <script src="sdk-touch/sencha-touch-all.js"></script>
    <!--<script src="sdk-touch/sencha-touch-all-debug.js"></script>-->
    <!--<script src="sdk-touch/sencha-touch-debug.js"></script>-->
    <script src="app.js"></script>
</head>
<body>
</body>

</html>

/**
 * Created by-PC on 2014/8/12.
 */
Ext.define('MyApp.controller.calculate', {
    toRad: function (d) {
        return d * Math.PI / 180;
    },
    getDisance: function (lat1, lng1, lat2, lng2) { //#lat为纬度, lng为经度, 一定不要弄错
        var dis = 0;
        var radLat1 = this.toRad(lat1);
        var radLat2 = this.toRad(lat2);
        var deltaLat = radLat1 - radLat2;
        var deltaLng = this.toRad(lng1) - this.toRad(lng2);
        var dis = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(deltaLat / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(deltaLng / 2), 2)));
        return dis * 6378137;
    }
});

/**
 * Created by-PC on 2014/8/12.
 */
Ext.define('MyApp.controller.controller', {
    extend: 'Ext.app.Controller',
    launch: function () {
        this.callParent(arguments);
        console.log('launch');
        //北京天安门的地理坐标
        var pk_la = 39.90960456049752;
        var pk_lo = 116.3972282409668;

        /*Ext.util.geolocation.getCurrentPosition(function (position) {
            console.log(position.coords.latitude);
            console.log(position.coords.longitude);
        });*/
        //var me_la = Ext.device.geolocation.latitude;
        //var me_lo = Ext.device.geolocation.longitude;

        navigator.geolocation.getCurrentPosition(function(position){
            //alert(position.coords.latitude+'+'+position.coords.longitude);

            //var me_la = Ext.Device.Geolocation.getLatitude;//不能实现获取
            //var me_lo = Ext.Device.Geolocation.getLongitude;//不能实现获取

            var me_la = position.coords.latitude;
            var me_lo = position.coords.longitude;

            var g = Ext.create('MyApp.controller.calculate');
            var distance = g.getDisance(pk_la, pk_lo, me_la, me_lo);

            //console.log(me_la);
            //console.log(me_lo);
            //console.log(distance);

            Ext.getCmp('locLabelID').setValue(distance+'米');

        });

    },
    init: function () {
        this.callParent();
        console.log('init1');
    }
});

/**
 * Created by-PC on 2014/8/12.
 */
Ext.define('MyApp.view.mainContainer',{
    extend:'Ext.Container',
    alias: 'widget.mainContainer',
    requires: ['Ext.Toolbar'],
    initialize:function(){
        this.callParent();

        var topToolBar={
            xtype:'toolbar',
            title:'直线距离',
            docked:'top',
            items:[
                {
                    xtype:'spacer'
                }
            ]
        };

        var locLabel = {
            xtype: 'textfield',
            name: 'title',
            label: '你与天安门的距离是:',
            id:'locLabelID'
        };

            this.add([
            topToolBar,
            locLabel

        ]);
    }
    /*config:{
        layout: {
            type: 'fit'
        }
    }*/
});

抱歉!评论已关闭.