javascript - 基本的 AngularJS 谷歌地图

标签 javascript google-maps angularjs angularjs-directive

我对 Angular 还很陌生,正试图尝试使用 Angular 谷歌地图。

我的要求。我有一个搜索栏,用于搜索餐馆。搜索结果 api 为我提供了用户搜索的餐厅的地址、纬度和经度。 我想要的是在 map 上显示一个映射餐厅位置的标记。

我试过了 https://github.com/nlaplante/angular-google-maps/ .它呈现 map 但不将 map 居中到我想要的纬度和经度。显然我试图从搜索 api 映射纬度和经度

我想知道是否有人有与我类似的用例并且有一个我可以效仿的例子。

感谢您的帮助。

最佳答案

更新:下面的链接似乎已经失效。使用这个:http://angular-ui.github.io/angular-google-maps/#!/

听起来这里的例子就是您要的:http://nlaplante.github.io/angular-google-maps/#!/demo

查看 DemoController 的源代码(http://nlaplante.github.io/angular-google-maps/js/demo-controller.js):

module.controller("DemoController", function ($scope, $location) {

        $scope.$watch("activeTab", function (newValue, oldValue) {
            if (newValue === oldValue) {
                return;
            }

            if ($location.path() != $scope.activeTab) {
                $location.path($scope.activeTab);
            }
        });

        $scope.getNavClass = function (item) {
            return item == $scope.activeTab ? "active" : "";
        };

        // default location
        $scope.center = {
            latitude: 45,
            longitude: -73
        };

        $scope.geolocationAvailable = navigator.geolocation ? true : false;

        $scope.latitude = null;
        $scope.longitude = null;

        $scope.zoom = 4;

        $scope.markers = [];

        $scope.markerLat = null;
        $scope.markerLng = null;

        $scope.addMarker = function () {
            $scope.markers.push({
                latitude: parseFloat($scope.markerLat),
                longitude: parseFloat($scope.markerLng)
            });

            $scope.markerLat = null;
            $scope.markerLng = null;
        };

        $scope.findMe = function () {

            if ($scope.geolocationAvailable) {

                navigator.geolocation.getCurrentPosition(function (position) {

                    $scope.center = {
                        latitude: position.coords.latitude,
                        longitude: position.coords.longitude
                    };

                    $scope.$apply();
                }, function () {

                });
            }   
        };
    });

您可以使用 $scope.center 告诉 map 在哪里居中。

希望这对您有所帮助!

关于javascript - 基本的 AngularJS 谷歌地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17128790/

相关文章:

javascript - 将值从页面表单传递到 Shopify Liquid 中的 JavaScript

javascript - Angular Google map 自定义标记

javascript - map 标记不出现(Javascript Google Maps API)

angularjs - Angular 在提升 promise.catch 之前将错误记录到控制台

javascript - Angular ng-repeat 列表值分割

javascript - React Transition Group无法触发生命周期

javascript - 我如何突出显示选定的文本并存储在数据库中以及当页面重新加载时突出显示选定的文本?

jquery - 不想使用 printThis 库显示打印预览弹出窗口

google-maps - 如何使用 Google Maps For Flutter 将初始相机位置设置为当前设备的纬度和经度

javascript - 从谷歌地图中删除现有的折线