2016-04-25 176 views
2

我正在使用角地圖來創建一個包含標記列表的地圖。 我有一個顯示標記的問題。雖然我沒有錯誤。標記不顯示在地圖上angularjs

這是我的代碼:

控制器

var app = angular.module('mapController', []); 

app.service('Map', function($q) { 

this.init = function() { 
    var options = { 
     center: new google.maps.LatLng(45.7154289, 4.9317724), 
     zoom: 14, 
     disableDefaultUI: true  
    } 
    this.map = new google.maps.Map(
     document.getElementById("map"), options 
    ); 
    this.places = new google.maps.places.PlacesService(this.map); 


    var markers = [{ 
    "title": 'Capgemini', 
    "lat": '45.7154289', 
    "lng": '4.9317724', 
    "description": 'Capgemini' 

    }]; 
var defaultMarkerColor = 'ff0000'; 
     var pinImage = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2|" + defaultMarkerColor); 

    // marker object for the marker 
    var marker = new google.maps.Marker({ 
    position: google.maps.LatLng(markers[0].lat,markers[0].lng), 
    map: this.map, 
    title: markers[0].title, 
    animation: google.maps.Animation.DROP, 
    icon: pinImage 
    }); 
} 

this.search = function(str) { 
    var d = $q.defer(); 
    this.places.textSearch({query: str}, function(results, status) { 
     if (status == 'OK') { 
      d.resolve(results[0]); 
     } 
     else d.reject(status); 
    }); 
    return d.promise; 
} 

this.addMarker = function(res) { 
    if(this.marker) this.marker.setMap(null); 
    this.marker = new google.maps.Marker({ 
     map: this.map, 
     position: res.geometry.location, 
     animation: google.maps.Animation.DROP 
    }); 
    this.map.setCenter(res.geometry.location); 
} 

}); 

app.controller('mapController', function($scope, Map) { 

$scope.place = {}; 

$scope.search = function() { 
    $scope.apiError = false; 
    Map.search($scope.searchPlace) 
    .then(
     function(res) { // success 
      Map.addMarker(res); 
      $scope.place.name = res.name; 
      $scope.place.lat = res.geometry.location.lat(); 
      $scope.place.lng = res.geometry.location.lng(); 
     }, 
     function(status) { // error 
      $scope.apiError = true; 
      $scope.apiStatus = status; 
     } 
    ); 
} 

$scope.send = function() { 
    alert($scope.place.name + ' : ' + $scope.place.lat + ', ' + $scope.place.lng);  
} 

Map.init(); 
}); 

HTML

<ion-content ng-controller="mapController" style="margin-top: 25px"> 

<div class="container"> 

<div id="map" libraries="places" data-tap-disabled="true"></div> 


<form name="searchForm" novalidate 
ng-submit="search()"> 
    <div class="input-group"> 
     <input name="place" type="text" class="form-control" 
     ng-model="searchPlace" required autofocus /> 
     <br> 
     <span class="input-group-btn"> 
      <button class="btn btn-primary" 
      ng-disabled="searchForm.$invalid">Search</button> 
     </span> 
    </div> 
    </form> 

</div> 

的地圖顯示正確,並集中在第我選擇的座標,但沒有標記。 我將不勝感激任何幫助。

回答

1

喜帶我一段到位的小提琴... :)

我做了這個變化,使其工作:

的標記:

var markers = [{ 
    "title": 'Capgemini', 
    "lat": 45.7154289, 
    "lng": 4.9317724, 
    "description": 'Capgemini' 
}]; 

然後標記定義

var marker = new google.maps.Marker({ 
    position:{lat: markers[0].lat, lng: markers[0].lng}, 
    map: this.map, 
    title: markers[0].title, 
    animation: google.maps.Animation.DROP, 
    icon: pinImage 
    }); 

最簡單的方法是使用數字作爲座標。

在這裏你可以找到fiddle 它不是超級乾淨,它是很多調試的東西,最重要的是,如果你可以初始化和看到你不真的需要它的地圖。

希望得到這個幫助