尝试获取简单的Google Maps示例以与AngularJS一起使用

埃文·R

我正在尝试将一个简单的Google Maps项目(实际上是从他们网站上的示例完成)迁移到AngularJS项目。首先,我要说我对AngularJS和Web开发人员都是陌生的,所以请客气:)我在https://github.com/eroth/angular-web-app有一个简单的项目(下面的主要文件) )从使用Google Maps教程的初始提交到尝试将其转换为AngularJS应用的过程,尽管我已经验证了$scope.init()MapController.js文件中的函数,但仍无法使其正常工作正在从我的map.html文件中调用。

我的问题有两个:是我的代码出了问题,还是我需要这样的东西(看起来非常好):http : //nlaplante.github.io/angular-google-maps/我正在将其合并到另一个分支的项目中,但是试图弄清楚现有代码是否存在问题,或者是否需要类似该库的内容才能使其与AngularJS一起使用。如果是后者,为什么呢?如果这是我犯的一个非常简单的错误,请提前道歉;正如我说的那样,尽管我确实阅读了一些AngularJS教程,但对这一切我还是很陌生,这看起来很棒。

这是我的map.html文件:

<!DOCTYPE html>
<html>
  <head>
    <meta charset = "utf-8">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />

    <!-- INCLUDE REQUIRED THIRD PARTY LIBRARY JAVASCRIPT AND CSS -->
    <script type="text/javascript" src="js/angular.min.js"></script>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/bootstrap-responsive.min.css">

    <!-- INCLUDE APPLICATION SPECIFIC CSS AND JAVASCRIPT -->
    <script type="text/javascript"
      src="http://maps.googleapis.com/maps/api/js?sensor=true&language=en">
    </script>
    <script type="text/javascript" src="js/web-app/app.js"></script>
    <script type="text/javascript" src="js/web-app/controllers/mainController.js"></script>
    <link rel="stylesheet" href="css/main.css">
  </head>
  <body>
    <div class="container main-frame" ng-app="WebApp" ng-controller ="mainController" ng-init="init()">
      <div id="map-canvas"/>
    </div>
  </body>
</html>

这是我的MapController.js文件:

app.controller("mainController", function($scope, $http){
$scope.initialLocation;
$scope.siberia = new google.maps.LatLng(60, 105);
$scope.newyork = new google.maps.LatLng(40.7463, -73.9913);
$scope.browserSupportFlag =  new Boolean();
$scope.map;
$scope.retina = window.devicePixelRatio > 1;

$scope.init = function () {
    var mapOptions = {
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    console.log('In main init');

    //first test——map uses hard-coded location, next will get user's location and pull deals
    $scope.map = new google.maps.Map(document.getElementById("map-canvas"),
        mapOptions);

    // Try W3C Geolocation (Preferred)
    if (navigator.geolocation) {
      $scope.browserSupportFlag = true;
      navigator.geolocation.getCurrentPosition(function(position) {
        $scope.initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
        var currentLat = position.coords.latitude;
        var currentLon = position.coords.longitude;
        $scope.map.setCenter($scope.initialLocation);
        // performApiCall(currentLat, currentLon);

        //definite custom map pin for user location & plot it on map
        var pinColor = "5ea9ff";
        var pinImage = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_spin&chld=0.6|0|" + pinColor + "|0|_|k",
        new google.maps.Size(25, 60),
        new google.maps.Point(0,0),
        new google.maps.Point(10, 24));
        var marker = new google.maps.Marker({
            position: $scope.initialLocation,
            map: $scope.map,
            icon: pinImage,
        });
      }, function() {
        handleNoGeolocation($scope.browserSupportFlag);
      });
    }

    // Browser doesn't support Geolocation
    else {
      $scope.browserSupportFlag = false;
      handleNoGeolocation($scope.browserSupportFlag);
    }

    function handleNoGeolocation(errorFlag) {
      if (errorFlag == true) {
        alert("Geolocation service failed.");
        $scope.initialLocation = newyork;
      } else {
        alert("Your browser doesn't support geolocation. We've placed you in Siberia.");
        $scope.initialLocation = siberia;
      }
      map.setCenter($scope.initialLocation);
    }
};

google.maps.event.addDomListener(window, 'load', $scope.init);
});

在此先感谢您的帮助!

苏维·维格纳拉

您的代码有几处错误。首先,您已经通过在DOM中定义init()作用域中函数mainControllerng-init

<div class="container main-frame" ng-app="WebApp" ng-controller ="mainController" ng-init="init()">

这意味着您不需要加载用于窗口的侦听器来调用$scope.init-,因为这将运行该函数两次,然后又两次初始化您的地图。删除侦听器。

其次,在一些实例中,实际上是$ scope对象(纽约州Ln 57,塞尔维亚Ln 60和地图Ln 62)的一部分,正在调用变量-因此将引发未定义的错误。

最后,您必须div在CSS中设置地图容器的高度才能实际查看地图。

#map-canvas { height : 200px; }
/* if you want to set the height as a percentage of the parent div, remember to give a height to the parent div as well */

至于您的问题的第二部分,我一定会考虑使用已经构建的内容。我没有使用过您链接到的库,但是如果您说它很好,为什么还要重新发明轮子呢?

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

尝试获取简单的Google Maps示例以与AngularJS一起使用

来自分类Dev

将AJAX与CodeIgniter一起使用的简单示例?

来自分类Dev

将Google Maps lib与Polymer一起使用

来自分类Dev

Flutter Google Maps和Firebase无法一起使用

来自分类Dev

关于如何将SwipeRefreshLayout与ListView一起使用的简单示例

来自分类Dev

如何将 Vue.js 与 json 数组一起使用 - 简单示例

来自分类Dev

OnClickListener无法与Google Maps片段顶部的按钮一起使用

来自分类Dev

Twitter Bootstrap列和Google Maps不能一起使用

来自分类Dev

OnClickListener不能与Google Maps片段顶部的按钮一起使用

来自分类Dev

Google Maps Marker Clusterer-如何将infoWindow与位置索引一起使用

来自分类Dev

AngularJS:尝试使过滤器与延迟的数据一起使用

来自分类Dev

尝试与ICustomTypeDescriptor一起使用DataGridView

来自分类Dev

使用Requirejs与Restangular和Twitter引导一起使用来获取Angularjs

来自分类Dev

与标记Google Maps javascript一起移动圆

来自分类Dev

使用Requirejs与Restangular和Twitter引导程序一起获取Angularjs

来自分类Dev

无法获取输入类型搜索以与OnsenUI / AngularJS一起使用

来自分类Dev

Google Map Fitbounds无法与angularjs ng-dialog一起使用

来自分类Dev

哪里可以找到angular-xeditable和ajax一起工作的简单示例

来自分类Dev

html5自动对焦无法与Google Maps v3信息窗口表单一起使用

来自分类Dev

制作我自己的数据库并与Google Maps API一起使用是否合法

来自分类Dev

获取AngularJS与其他javascript一起玩

来自分类Dev

获取angularjs应用程序以与另一个应用程序(流星)一起使用

来自分类Dev

尝试与PHP cURL一起使用时无法获取Facebook页面源代码

来自分类Dev

将Google Maps google-maps-react npm包与react-intersection-observer一起使用以增加灯塔得分

来自分类Dev

简单的JS / JSON无法与Angular一起使用

来自分类Dev

将尝试捕获块与ELMAH一起使用

来自分类Dev

尝试将DOMDocument :: loadHTMLFile与生成的url一起使用

来自分类Dev

尝试将$ scope。$ watch与controllerAs一起使用

来自分类Dev

尝试与`with`一起使用时,NetLogo中的错误消息

Related 相关文章

  1. 1

    尝试获取简单的Google Maps示例以与AngularJS一起使用

  2. 2

    将AJAX与CodeIgniter一起使用的简单示例?

  3. 3

    将Google Maps lib与Polymer一起使用

  4. 4

    Flutter Google Maps和Firebase无法一起使用

  5. 5

    关于如何将SwipeRefreshLayout与ListView一起使用的简单示例

  6. 6

    如何将 Vue.js 与 json 数组一起使用 - 简单示例

  7. 7

    OnClickListener无法与Google Maps片段顶部的按钮一起使用

  8. 8

    Twitter Bootstrap列和Google Maps不能一起使用

  9. 9

    OnClickListener不能与Google Maps片段顶部的按钮一起使用

  10. 10

    Google Maps Marker Clusterer-如何将infoWindow与位置索引一起使用

  11. 11

    AngularJS:尝试使过滤器与延迟的数据一起使用

  12. 12

    尝试与ICustomTypeDescriptor一起使用DataGridView

  13. 13

    使用Requirejs与Restangular和Twitter引导一起使用来获取Angularjs

  14. 14

    与标记Google Maps javascript一起移动圆

  15. 15

    使用Requirejs与Restangular和Twitter引导程序一起获取Angularjs

  16. 16

    无法获取输入类型搜索以与OnsenUI / AngularJS一起使用

  17. 17

    Google Map Fitbounds无法与angularjs ng-dialog一起使用

  18. 18

    哪里可以找到angular-xeditable和ajax一起工作的简单示例

  19. 19

    html5自动对焦无法与Google Maps v3信息窗口表单一起使用

  20. 20

    制作我自己的数据库并与Google Maps API一起使用是否合法

  21. 21

    获取AngularJS与其他javascript一起玩

  22. 22

    获取angularjs应用程序以与另一个应用程序(流星)一起使用

  23. 23

    尝试与PHP cURL一起使用时无法获取Facebook页面源代码

  24. 24

    将Google Maps google-maps-react npm包与react-intersection-observer一起使用以增加灯塔得分

  25. 25

    简单的JS / JSON无法与Angular一起使用

  26. 26

    将尝试捕获块与ELMAH一起使用

  27. 27

    尝试将DOMDocument :: loadHTMLFile与生成的url一起使用

  28. 28

    尝试将$ scope。$ watch与controllerAs一起使用

  29. 29

    尝试与`with`一起使用时,NetLogo中的错误消息

热门标签

归档