离子pubnub实时地理定位跟踪不工作

时间:2015-12-12 13:53:54

标签: javascript ionic pubnub



var exampleApp= angular.module('starter', ['ionic'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})
exampleApp.controller('MapController', function($scope, $ionicLoading) {
 var map;
    var map_marker;
    var lat = null;
    var lng = null;
    var lineCoordinatesArray = [];

    // sets your location as default
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(function(position) { 
        var locationMarker = null;
        if (locationMarker){
          // return if there is a locationMarker bug
          return;
        }

        lat = position.coords["latitude"];
        lng = position.coords["longitude"];

        // calls PubNub
        pubs();

        // initialize google maps
        google.maps.event.addDomListener(window, 'load', initialize());
      },
      function(error) {
        console.log("Error: ", error);
      },
      {
        enableHighAccuracy: true
      }
      );
    }    


    function initialize() {
      console.log("Google Maps Initialized")
      map = new google.maps.Map(document.getElementById('map'), {
        zoom: 15,
        center: {lat: lat, lng : lng, alt: 0}
      });

      map_marker = new google.maps.Marker({position: {lat: lat, lng: lng}, map: map});
      map_marker.setMap(map);
    }

    // moves the marker and center of map
    function redraw() {
      map.setCenter({lat: lat, lng : lng, alt: 0})
      map_marker.setPosition({lat: lat, lng : lng, alt: 0});
      pushCoordToArray(lat, lng);

      var lineCoordinatesPath = new google.maps.Polyline({
        path: lineCoordinatesArray,
        geodesic: true,
        strokeColor: '#2E10FF',
        strokeOpacity: 1.0,
        strokeWeight: 2
      });
      
      lineCoordinatesPath.setMap(map);
    }


    function pushCoordToArray(latIn, lngIn) {
      lineCoordinatesArray.push(new google.maps.LatLng(latIn, lngIn));
    }
    

    function pubs() {
      pubnub = PUBNUB.init({
        publish_key: 'pub-c-2df55398-9110-4b2b-bdab-97bf634be349',
        subscribe_key: 'sub-c-6b2c387c-7148-11e4-86a8-02ee2ddab7fe'
      })

      pubnub.subscribe({
        channel: "mymaps",
        message: function(message, channel) {
          console.log(message)
          lat = message['lat'];
          lng = message['lng'];
          //custom method
          redraw();
        },
        connect: function() {console.log("PubNub Connected")}
      })
    }

    });
 

<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->

    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="http://cdn.pubnub.com/pubnub-3.7.1.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>

    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3&sensor=false&language=en"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
  </head>
  <body ng-app="starter">

    <ion-pane>
      <ion-header-bar class="bar-stable">
        <h1 class="title">Ionic Blank Starter</h1>
      </ion-header-bar>
     <ion-content ng-controller="MapController">
      <div id="map" data-tap-disabled="true"></div>
      </ion-content>
    </ion-pane>

  </body>
</html>
&#13;
&#13;
&#13; 上面的代码包含app.js代码,其中我把我的api密钥和那些地理位置javascript代码我遇到的错误是当我运行应用程序时标记不会自动更新并移动不像pubnub教程是juz check {{3}请帮助我

1 个答案:

答案 0 :(得分:1)

Ionic Geo位置测试

您收到安全错误。用户的位置和位置受浏览器保护。您需要从HTTP Server运行项目。我建议您使用一个命令运行HTTPS服务器:

margin
  

您的静态文件将在https://0.0.0.0:4443/index.html上提供。

或者,您可以通过运行此命令来运行不安全的HTTP Server:

## HTTP(S) Secure Server
python <(curl -L https://goo.gl/Rrko89)
  

您的不安全静态文件将在http://0.0.0.0:8000/index.html上提供。

现在,当您在下方看到我的屏幕截图时,它会正常工作。

Ionic Geo Location Tracking

要在Cordova PhoneGap中运行代码,您需要启用权限https://cordova.apache.org/docs/en/2.3.0/cordova/geolocation/geolocation.html

Ionic Cordova PhoneGap Geo Permissions

的Android

  

应用程序/ RES / XML / config.xml中

## HTTP Insecure Server
python -m SimpleHTTPServer
  

应用程序/ AndroidManifest.xml中

<plugin name="Geolocation" value="org.apache.cordova.GeoBroker" />

的iOS

  

config.xml中

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />