【问题标题】:AngularJS show pre defined coordinates of user on the map [duplicate]AngularJS在地图上显示用户的预定义坐标[重复]
【发布时间】:2016-11-14 09:18:09
【问题描述】:

登录时我会在地图上显示用户的预定义坐标,这是我的地图

map.html

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<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="https://maps.googleapis.com/maps/api/js?key=AIzaSyB16sGmIekuGIvYOfNoW9T44377IU2d2Es&sensor=true"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
    <script src="js/controllers.js"></script>
    <script src="js/directives.js"></script>
  </head>
  <body ng-app="starter" ng-controller="MapCtrl">
    <ion-header-bar class="bar-stable">
      <h1 class="title">Map</h1>
    </ion-header-bar>

    <ion-content scroll="false">
      <map on-create="mapCreated(map)"></map>
    </ion-content>

    <ion-footer-bar class="bar-stable">
      <a ng-click="centerOnMe()" class="button button-icon icon ion-navigate"></a>
    </ion-footer-bar>
  </body>
</html>

这是我的控制器

controllers.js

angular.module('starter.controllers', [])
.controller('MapCtrl', function($scope, $ionicLoading, $cordovaGeolocation) {
$scope.mapCreated = function(map) {
  $scope.centerOnMe = function () {
    console.log("Centering");
    if (!$scope.map) {
      return;
    }
    $scope.loading = $ionicLoading.show({
      content: 'Getting current location...',
      showBackdrop: false
    });
 navigator.geolocation.getCurrentPosition(function (pos) {
      console.log('Got pos', pos);
      $scope.map.setCenter(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));
      $scope.loading.hide();
    }, function (error) {
      alert('Unable to get location: ' + error.message);
    });
  };
});
.controller('MapCtrl', function ($scope,geolocation) {
    geolocation.getLocation().then(function(data){
      $scope.coords = {lat:data.coords.latitude, long:data.coords.longitude};
    });
});

loginController.js

var app=angular.module('starter', ['ionic', 'starter.controllers', 'starter.directives', 'ui.router'])
    .config(function($stateProvider, $urlRouterProvider, $httpProvider, $locationProvider){
    console.log('test');

    $stateProvider
       .state('map', {
        url: '/map',
        templateUrl: 'map.html',
        controller: 'MapCtrl'
})
 $urlRouterProvider.otherwise('/index');
     });


app.controller('loginCtrl', function ($scope,$http,$location,$state) 
 { 

    $scope.data = {};
    $scope.postLogin = function ()
    {

        var data = 
        {
                email: $scope.data.email,
                password: $scope.data.password,
                lat: $scope.data.latitude,
                lang: $scope.data.langitude
        };

       console.log('bonjour');
        $http.post("http://localhost/authproject/public/api/auth/login", data)
        .then(
           function(response){
             // success callback
             console.log('success');
             $location.path('/map');
             $scope.data.latitude=response.latitude;
             $scope.data.langitude=response.langitude;
             $scope.map.setCenter(new google.maps.LatLng(data.lat, data.long));

           }, 
           function(response){
             // failure callback
            
             console.log('error');
             alert('invalid user ');
             //$location.path('/index');
             
           }
        );

        

    }
    
});

我应该在数据库中任何用户的预定义位置添加什么?

【问题讨论】:

  • 您到底需要什么?从数据库中获取数据然后在地图中使用的代码?
  • 我的数据库中有很多用户,如果我使用任何用户登录我想要访问我在地图中预定义的静态地理位置,或者我应该使用基于地址的谷歌地图图像
  • 你应该这样做:$scope.map.setCenter(new google.maps.LatLng(response.latitude, response.longitude));.
  • 但其次你确定你在登录控制器的$scope 中有有效的map 对象吗?我认为地图在其他视图中。因此,最好有一些服务将用户数据保存在内存中 (currentUserService),然后在地图控制器中使用它:currentUser.latitude 等。
  • 你是对的,对象映射在登录控制器中无效,我是初学者,我不知道我在哪里创建 currentUserService 的服务

标签: angularjs google-maps-api-3 ionic-framework


【解决方案1】:

如果您只想将地图设置为所有用户的相同位置,那么您可以这样做:

var lat = 51.123456;
var lgt = 5.123456;
$scope.map.setCenter(new google.maps.LatLng(lat, lgt));

如果您想将其设置为分别为每个用户定义的位置,则首先访问数据库并获取用户的纬度和经度并在那里设置。

如果我理解你的问题,那么你根本不需要navigator.geolocation.getCurrentPosition,因为你不想使用浏览器提供的位置。

编辑:

您关于获取数据的问题非常广泛,但总体思路如下。您需要创建一些处理数据库的服务(例如dataService),您可以使用它来设置地图:

dataService.loadUser(currentUserId).then(function(data) {
    $scope.map.setCenter(new google.maps.LatLng(data.latitude, data.longitude));
});

这表明您还需要知道当前用户的 id 或用户名或您使用的任何身份。

【讨论】:

  • 考虑到您的回复,我想将其设置为分别为每个用户定义的位置,我将在数据库中为用户添加纬度和经度,然后应该在 controllers.js 中添加什么功能跨度>
  • 有人能回答我吗
  • 查看我的更新答案。
  • 想你@Episodex 我在我的登录控制器中添加了这一行
  • 我在问题中添加了 loginController.js
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-18
  • 1970-01-01
  • 2014-07-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多