简体   繁体   English

Angularjs Google Map添加标记

[英]Angularjs Google Map Add Markers

I am working on a project which need to place additional marker based on the latlng retrieved from database. 我正在一个项目中,该项目需要根据从数据库中检索到的latlng放置其他标记。 The map load successfully but somehow i just can't make marker show on the map. 地图加载成功,但是以某种方式我无法使标记显示在地图上。

Here is my code 这是我的代码

JAVASCRIPT 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();
});

Is there anything i forgot to add or ?? 有什么我忘了补充的吗?

see this http://jsfiddle.net/pc7Uu/3153/ 看到这个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)
});

You were calling your markers before the Map was initialized, replace values by your server values and it shall work. 您在地图初始化之前调用了标记,将值替换为服务器值即可使用。

Hope this solves your problem 希望这能解决您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM