簡體   English   中英

Angularjs Google Map添加標記

[英]Angularjs Google Map Add Markers

我正在一個項目中,該項目需要根據從數據庫中檢索到的latlng放置其他標記。 地圖加載成功,但是以某種方式我無法使標記顯示在地圖上。

這是我的代碼

JAVASCRIPT

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

app.service('Map', function($q) {
  this.init = function() {
      navigator.geolocation.getCurrentPosition(function(position) {
        var MapOptions = {
          center: new google.maps.LatLng(38.431934, 141.309402),
          zoom: 16,
          disableDefaultUI: true,
          draggable: true,
          mapTypeId: google.maps.MapTypeId.ROADMAP,
        };
        this.map = new google.maps.Map(document.getElementById("map"), MapOptions);
        this.places = new google.maps.places.PlacesService(this.map);
      });
    }  
});

app.controller('MainCtrl', function($scope, $resource, Map) {
  $scope.fetchdata = {};
  var Fetchdata = $resource('http://localhost:8080/fetchnote');
  Fetchdata.query(function(results) {
    $scope.fetchdata = results;
    angular.forEach($scope.fetchdata, function(value, index) {
      var lat = value.latitude;
      var lng = value.longitude;
      addtomap(lat, lng)
    })
  })

  function addtomap(lat, lng, icon) {
    var marker = new google.maps.Marker({
      position: new google.maps.LatLng(lat, lng),
      map: Map.map
    });
  }
  Map.init();
});

有什么我忘了補充的嗎?

看到這個http://jsfiddle.net/pc7Uu/3153/

 Map.init();
 angular.forEach($scope.fetchdata, function(value, index) {
  //alert(value);
  var lat = value.latitude;
  var lng = value.longitude;
  addtomap(lat, lng)
});

您在地圖初始化之前調用了標記,將值替換為服務器值即可使用。

希望這能解決您的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM