【问题标题】:Google maps with angular [closed]带角度的谷歌地图[关闭]
【发布时间】:2015-12-13 18:27:20
【问题描述】:

我想做一个项目,我需要集成 google maps api。我需要自动完成、本地化并在地图上绘制路线。我怎么能用 Angular 做到这一点,你能给我推荐一个图书馆吗?或者我怎样才能用 google maps api for javascript 来做这个。 该项目是使用 yeoman-fullstack 生成的。

谢谢。

【问题讨论】:

标签: javascript angularjs google-maps google-api yeoman


【解决方案1】:

首先,如果您想让 Google Maps APIAngularJS 一起使用,您有两种解决方案:

我将向您展示如何从头开始变得简单。

这是一个简单的 AngularJS 应用程序示例,仅用于学习使用此类 API。我做了这个小小的 AngularJS 练习,以便在更大的应用程序中使用它。

Index.html:

<html ng-app="myApp"> <!--Your App-->
  <head>
    <title>AngularJS-Google-Maps</title>
    <link rel="stylesheet" href="style.css">
    <script src="../lib/angular.min.js"></script>
    <script src="app.js"></script>

    <!-- You have to look for a Maps Key, you can find it on Google Map            
         Api's website if you pay a bit of attention-->
    <script type="text/javascript"
            src="https://maps.googleapis.com/maps/api/js?key=
       YourKey&sensor=false">
    </script>

  </head>
  <body>
    <!--Custom directive my-maps, we will get our map in here-->
    <my-maps id="map-canvas"></my-maps>

  </body>
</html>

app.js:

var myApp = angular.module("myApp",[]);

//Getting our Maps Element Directive
myApp.directive("myMaps", function(){
    return{
        restrict:'E', //Element Type
        template:'<div></div>', //Defining myApp div
        replace:true, //Allowing replacing
        link: function(scope, element, attributes){

            //Initializing Coordinates
            var myLatLng = new google.maps.LatLng(YourLat,YourLong); 
            var mapOptions = {
                center: myLatLng, //Center of our map based on LatLong Coordinates
                zoom: 5, //How much we want to initialize our zoom in the map
                //Map type, you can check them in APIs documentation
                mapTypeId: google.maps.MapTypeId.ROADMAP 
                };

            //Attaching our features & options to the map
            var map = new google.maps.Map(document.getElementById(attributes.id),
                          mapOptions);
            //Putting a marker on the center
            var marker = new google.maps.Marker({
                position: myLatLng,
                map: map,
                title:"My town"
            });
            marker.setMap(map); //Setting the marker up
        }   
    };
});

style.css:

//Just giving our container Height and Width + A Red border
#map-canvas{
        height: 400px;
        width:  700px;
        border: 2px solid red;
}

这只是开始它的一个非常基本的方法,我什至没有使用控制器,即使你真的应该在 AngularJS 中使用它们。

这是一个有效的Plunk 我忍受了这段代码。

您可以通过多种不同方式使用 Google Maps API,只需查看文档或查看 StackOverflow 上的此处。

现在,如果您想管理 2 个位置、标记等之间的路线,您应该查看:

最后,您可以查看@Shreerang Patwardhan 使用折线制作的this working JSFiddle(您一直想要的路线)。

对于未来的实现,确保将您的 myMaps.js 指令放在一个单独的文件中并在您的应用模块中注入它具有一个依赖项,以便获得 DRY(不要重复自己)和可维护应用程序使用相同的工作指令作为涉及 Google 地图的 Angular 应用程序的起点。例如,您只需更改坐标或添加一些新地图选项即可启动未来需要 Google 地图的应用程序。

你应该得到类似的东西:

myApp.directive("myMap", function(){ . . . }); //Goes in a Separated File

var myApp = angular.module("myApp",['myMaps']); //Dependency Injection

希望我对您有所帮助。

【讨论】:

    【解决方案2】:

    您可以使用 Angular 谷歌地图。 Angular Google Maps 是一组用 CoffeeScript 和 Javascript 编写的指令(angular-ui 的一部分),将 Google Maps 集成到 AngularJS 应用程序中。当然还有很多其他的指令可以达到这个目的。

    https://angular-ui.github.io/angular-google-maps/

    【讨论】:

    • 感谢您的回复,但使用此 api 我没有找到如何绘制路线。你知道我该怎么做吗?
    猜你喜欢
    • 2017-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多