2014-07-15 96 views
0

我正在一個頁面上有一個包含許多標記和標記標題一側的列表的地圖。這個例子幫助我開始。 http://jsfiddle.net/svigna/pc7Uu/谷歌地圖Angular Js

我對每個標記位置都有圖像和描述,只有在單擊相關標記時,我纔會在旁邊列表中顯示這些圖像和描述。

關於如何實現這一邏輯的任何想法?謝謝。

這裏是我的HTML

<div ng-app="mapsApp" ng-controller="MapCtrl"> 
<div id="map"></div> 
<div id="class" ng-repeat="marker in markers | orderBy : 'title'"> 
    <a href="#" ng-click="openInfoWindow($event, marker)">{{marker.title}}</a> 
    <img ng-src="{{marker.image}}" alt="\{\{album.album\}\} Cover Image" width="300px;" height="300px;"> 
</div> 
</div> 

這裏是我的角度

//Angular App Module and Controller 
angular.module('mapsApp', []) 
.controller('MapCtrl', function ($scope) { 

var mapOptions = { 
    zoom: 9, 
    center: new google.maps.LatLng(10.51, 7.432), 
    mapTypeId: google.maps.MapTypeId.ROADMAP, 
    scrollwheel: false 
} 

$scope.map = new google.maps.Map(document.getElementById('map'), mapOptions); 

$scope.markers = []; 

var infoWindow = new google.maps.InfoWindow({ 
maxWidth: 200 
}); 

var createMarker = function (info){ 

    var marker = new google.maps.Marker({ 
     map: $scope.map, 
     position: new google.maps.LatLng(info.lat, info.long), 
     title: info.location, 
     image: info.img 
    }); 
    marker.content = '<div class="infoWindowContent">' + info.desc + '</div>'; 

    google.maps.event.addListener(marker, 'click', function(){ 
     infoWindow.setContent('<h2>' + marker.title + '</h2>' + marker.content); 
     infoWindow.open($scope.map, marker); 
    }); 

    $scope.markers.push(marker); 

} 

for (i = 0; i < cities.length; i++){ 
    createMarker(cities[i]); 
} 

$scope.openInfoWindow = function(e, selectedMarker){ 
    e.preventDefault(); 
    google.maps.event.trigger(selectedMarker, 'click'); 
} 

}); 

這裏是我的JSON

var cities = [ 
{ 
    location : 'Unguwar Rimi', 
    desc : "<div id='hook' class='image'><img src=\"images/wells/KA KAF 001 Unguwar Rimi/IMG_4569.JPG\"><br><strong>Kaduna, Nigeria</strong><br>Village: Unguwar Rimi<br>Completed: 2011<br><p>'This borehole is a good blessing, our labor and stress is reduced.'</p></div>", 
    img: "images/wells/KA KAF 001 Unguwar Rimi/IMG_4569.JPG", 
    lat : 10.52, 
    long : 7.43 
}, 
{ 
    location : 'Gidan Bege Kadarko Kagoro', 
    desc : "<div id='hook' class='image'><img src=\"images/wells/KA KAU 001 Gidon Bege Kadarko Kagoro/IMG_4556.JPG\"><br><strong>Kaduna, Nigeria</strong><br>Village: Unguwar Rimi<br>Completed: 2008<br><p>'God is great, the village no longer has a water problem and we never run short of water.'</p></div>", 
    img: "images/wells/KA KAU 001 Gidon Bege Kadarko Kagoro/IMG_4556.JPG", 
    lat : 10.53, 
    long : 7.27 
}, 
{ 
    location : 'ECWA Seminary Kagoro', 
    desc : "<div id='hook' class='image'><img src=\"images/wells/KA KAU 002 ECWA Seminary Kagoro/IMG_8123.JPG\"><br><strong>Kaduna, Nigeria</strong><br>Village: ECWA Seminary Kagoro<br>Completed: 2008<br><p>'God is our Savior. He blessed us with this borehole which provides water to our staff and students'</p></div>", 
    img: "images/wells/KA KAU 002 ECWA Seminary Kagoro/IMG_8123.JPG", 
    lat : 9.69, 
    long : 8.39 
} 
]; 

回答

1

您需要將範圍變量設置爲click事件中的某些內容。

在你的控制器:

 $scope.selectedMarker = {}; 
    // etc 
    google.maps.event.addListener(marker, 'click', function(){ 

     infoWindow.setContent('<h2>' + marker.title + '</h2>' + marker.content); 
     $scope.selectedMarker = marker; 
     $scope.$apply(); 
     infoWindow.open($scope.map, marker); 
    }); 

在您的看法,您可以:

<h1>{{selectedMarker.title}}</h1> 

這是一個完整的演示:http://jsfiddle.net/pc7Uu/194/

此外,如果你想顯示HTML(標記的內容)在你包含的角度版本(1.1.1)中,你需要包含ngSanitize。

http://jsfiddle.net/xVGP5/1/

+0

謝謝!那很完美! – byrdr

+0

沒問題,你可能會考慮使用角度google map指令。 https://github.com/angular-ui/ui-map – lucuma