谷歌地图有角度

时间:2015-12-13 18:27:20

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

我想制作一个项目,我需要整合google maps api。我需要自动完成,本地化并在地图上绘制路线。我怎么能用角度做这个,你能为我推荐一个图书馆吗?或者我如何使用谷歌地图api为javascript做到这一点。 这个项目是使用yeoman-fullstack生成的。

感谢。

2 个答案:

答案 0 :(得分:6)

首先,如果您希望 Google Maps API AngularJS 一起使用,您有两个解决方案:

我将向您展示如何从头开始轻松实现。

这是一个简单的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个地点,标记等之间的路线,您应该查看:

最后,您可以使用Polylines(您想要的长途路线)查看@Shreerang Patwardhan制作的this working JSFiddle

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

你应该得到类似的东西:

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

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

我希望我一直很有帮助。

答案 1 :(得分:2)

您可以使用Angular Google地图。 Angular Google Maps是一套用CoffeeScript和Javascript编写的指令(angular-ui的一部分),它将Google Maps集成到AngularJS应用程序中。当然,还有许多其他指令用于此目的。

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