<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Simple Polylines</title>
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: {lat: 0, lng: -180},
mapTypeId: google.maps.MapTypeId.TERRAIN
});
var lat1=[37.772,37.772];
var lng1=[-122.214,-122.214];
var lat2= [-27.467,67.999];
var lng2= [-153.027,-126.788];
for(var i=0; i<lat1.length; i++)
{
var flightPlanCoordinates = [
{lat: lat1[i], lng: lng1[i]},
{lat: lat2[i], lng: lng2[i]}
];
var flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
flightPath.setMap(map);
}
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB3wFd2SjiCAqbFrm8GgeZJ0RuGHSguDSQ&signed_in=true&callback=initMap"></script>
</body>
</html>
这是我在不同起点和终点之间绘制折线的代码。这很好用。现在我需要将它与我的角度代码集成,后者从我的数据库中获取数据。
var app = angular.module('myApp', []);
angular.module('myApp', []).controller('orderCtrl', function($scope,$http,$interval){
load_userrequest();
function load_userrequest(){
console.log("in angular user request view");
$http.get('http://localhost:3001/bill').success(function(data){
console.log(data);
$scope.users=data;
});
};
});
如何整合这些代码?我想在$ scope.users = data之后包含我的initMap函数。当我这样做时,我得到一个错误,说window.init没有定义
答案 0 :(得分:0)
以下转换的AngularJS示例显示了如何从外部端点读取坐标:
angular.module('mapsApp', [])
.controller('MapCtrl', function ($scope,$http) {
$scope.map = new google.maps.Map(document.getElementById('map_div'), {
zoom: 3,
center: {lat: 0, lng: -180},
mapTypeId: google.maps.MapTypeId.TERRAIN
});
$http.get('https://gist.githubusercontent.com/vgrem/61352c9edea7bec8d1e0/raw/20673c41c76cfffdb827d65a363b8ba4b0fc15e7/flightPlanCoordinates.json')
.success(function(data) {
data.forEach(function(coords){
var flightPath = new google.maps.Polyline({
path: coords,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
flightPath.setMap($scope.map);
});
});
});
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map_div {
height: 600px;
}
<script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.js'></script>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div ng-app="mapsApp" ng-controller="MapCtrl">
<div id="map_div"></div>
</div>