用户计算距离traveld时出错

时间:2016-11-18 09:09:01

标签: angularjs ionic-framework

我是离子和棱镜的新手,我正在开发一个跟踪应用程序,我无法计算跟踪距离并将其存储在数据库中。

这是我的index.html

 <!DOCTYPE html>
<html  ng-app="starter">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>
    <link rel="manifest" href="manifest.json">
    <link href="css/ionic.app.css" rel="stylesheet">
    <script src="lib/ionic/js/ionic.bundle.js"></script>
    <script src="lib/ngCordova/ng-cordova.js"></script>
    <script src="cordova.js"></script>
    <script src="js/app.js"></script>
   <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAHe3efHByU2G1ECBpenC8ZpiXlpO7GFXA "></script>
  </head>
  <body ng-controller="MapCtrl">
    <ion-pane>
    <ion-header-bar class="bar-stable">
      <h1 class="title">Ionic Maps</h1>
    </ion-header-bar>
    <ion-content ng-init="first(User)">
        <div class="padding">
          <button class="button button-large button-calm" ng-click="starttrack(User)">Start</button>
          <button class="button button-large button-assertive" ng-click="endtrack(User)">End</button>
        </div>
        <div>coordinates = {{getText(User)}}</div>

        <button class="button button-small button-balanced" ng-click="dist()">Show distance</button>
    </ion-content>
  </ion-pane>
  </body>
</html>

这是我的app.js

.factory('AllServices', function($http) {
    return {
        drivertrackFunction: function(User) {

            var data = { coordinates: User.coordinates,
                        dis: User.dis};

            var link = 'http://localhost/track.php';         
            return $http.post(link,data);
        }
    };
})
.controller('MapCtrl', function($scope, filterFilter, $state, $cordovaGeolocation, $interval, AllServices) {

    $scope.User = {};
    $scope.User.mysrclat= 0; 
    $scope.User.mysrclong = 0;
    $scope.User.timer = "";
    $scope.addstring=[];
    $scope.addstring1 = [];

    $scope.User.string1="";
    $scope.User.coordinates=undefined;
    $scope.User.string3="";

    $scope.lat1 = "";
    $scope.lon1 = "";

    $scope.lat2 = "";
    $scope.lon2 = "";
    $scope.User.d = "";
    $scope.User.totalDis="";
    $scope.originals = [];
    $scope.User.temp = 0;
    $scope.points = "";
    $scope.User.pLen = "";
    $scope.User.len = "";
    $scope.User.origin1 = "";
    $scope.User.destinationB = "";
    $scope.User.geocoder = "";
    $scope.User.service = "";
    $scope.User.originList ="";
    $scope.User.destinationList = "";
    $scope.User.outputDiv = "";
    $scope.User.TotDis = "";
    $scope.User.TotDur = "";
    $scope.User.dis = 0;
    $scope.first = function(User) {
        if(navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function (position) {
                $scope.User.mysrclat = position.coords.latitude;
                $scope.User.mysrclong = position.coords.longitude;

                $scope.User.string1 = $scope.User.mysrclat + "," + $scope.User.mysrclong;
                console.log($scope.User.mysrclat);

            });
        }
    };
    $scope.track = function(User) {

        $scope.first(User);
    };

    function distance(User) {
        var R = 6371; // km (change this constant to get miles)
        var dLat = ($scope.lat2-$scope.lat1) * Math.PI / 180;
        var dLon = ($scope.lon2-$scope.lon1) * Math.PI / 180;
        var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos($scope.lat1 * Math.PI / 180 ) * Math.cos($scope.lat2 * Math.PI / 180 ) * Math.sin(dLon/2) * Math.sin(dLon/2);
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        $scope.User.d = R * c;
        if ($scope.User.d>1) {
            return Math.round($scope.d)+"km";
        }
        else {
            if ($scope.User.d<=1) {
                return Math.round($scope.d*1000)+"m";
            }
        }
        return $scope.User.d;
    };
    $scope.starttrack = function(User) {

        $scope.track(User);
        $scope.addstring.push($scope.User.string1);
        $scope.addstring1.push($scope.User.string1);

        var values = $scope.User.string1.split(","); 
        $scope.lat1 = $scope.User.mysrclat;
        $scope.lon1 = $scope.User.mysrclong;
        $scope.lat2 = values[0];
        $scope.lon2 = values[1];

        distance();

        $scope.User.timer = $interval(function() {

            if($scope.addstring != "") {
                $scope.track(User);

                $scope.lat1 = $scope.lat2;
                $scope.lon1 = $scope.lon2;
                $scope.lat2 = $scope.User.mysrclat;
                $scope.lon2 = $scope.User.mysrclong;

                distance();
                console.log($scope.User.d);
                $scope.User.dis = $scope.User.dis + $scope.User.d;

                $scope.addstring.push("|" + $scope.User.string1);  
                $scope.addstring1.push($scope.User.string1); 
            }

        }, 10000);

    };

    $scope.getText = function(User){
       return $scope.addstring.join("");
    };

    $scope.endtrack = function(User, d) {

        if(angular.isDefined($scope.User.timer)) {
            $interval.cancel($scope.User.timer);
            $scope.User.timer = undefined;

            $scope.User.string2 = $scope.addstring[0];

            for(var i=1; i<($scope.addstring).length; i++) {
                $scope.User.string2 = $scope.User.string2 + $scope.addstring[i];

            }

            $scope.User.coordinates = $scope.User.string2;

            $scope.User.dis = $scope.User.dis + " KM";
            console.log($scope.User.dis);

            AllServices.drivertrackFunction(User)
            .then(function(response) {

            })
            .catch(function(error) {
                console.log(error);
            });   
        }
    };
    $scope.dist = function() {
        distance();
        console.log($scope.User.d, "Km");
    };
})

这是我的PHP代码

<?php 
    include("connection.php");
    $data = json_decode(file_get_contents("php://input"));
    $coordinates = $data->coordinates;
    $dis = $data->dis;


    $q = "INSERT INTO track (Coordinates, Distance) VALUES (:coordinates, :dis)";
        $query = $db->prepare($q);
        $execute = $query->execute(array(
            ":coordinates" => $coordinates,
            ":dis" => $dis
        ));

    echo json_encode($data);
?>

0 个答案:

没有答案