谷歌地图api V3标记

时间:2014-12-19 11:26:04

标签: javascript jquery google-maps google-maps-api-3

我的谷歌地图有问题,我不能在某些地方放置标记。我的想法是找到我的位置并在其上放置标记,然后将标记放在其他位置,但其他位置上的标记未显示在地图上。

function success(position) {
      var mapcanvas = document.createElement('div');
      mapcanvas.id = 'mapcontainer';
      mapcanvas.style.height = '550px';
      mapcanvas.style.width = '960px';

      document.querySelector('article').appendChild(mapcanvas);

      var coords = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

      var options = {
          zoom: 15,
          center: coords,
          mapTypeControl: false,
          navigationControlOptions: {
              style: google.maps.NavigationControlStyle.SMALL
          },
          mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      var map = new google.maps.Map(document.getElementById("mapcontainer"), options);

      var marker = new google.maps.Marker({
          position: coords,
          map: map,
          title: "Vasa lokacija"
      });
  }

  if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(success);
  } else {
      error('Geo Location is not supported');
  }
  var locations = [
      ['Banja Luka', 44.766666699999990000, 17.183333299999960000, 4],
      ['Tuzla', 44.532841000000000000, 18.670499999999947000, 5],
      ['Zenica', 44.203439200000000000, 17.907743200000027000, 3],
      ['Sarajevo', 43.850000000000000000, 18.250000000000000000, 2],
      ['Mostar', 43.333333300000000000, 17.799999999999954000, 1]
  ];
  var infowindow = new google.maps.InfoWindow();

  var marker, i;

  for (i = 0; i < locations.length; i++) {
      marker = new google.maps.Marker({
          position: new google.maps.LatLng(locations[i][1], locations[i][2]),
          map: map
      });

      google.maps.event.addListener(marker, 'click', (function (marker, i) {
          return function () {
              infowindow.setContent(locations[i][0]);
              infowindow.open(map, marker);
          }
      })(marker, i));
  }
  }

2 个答案:

答案 0 :(得分:0)

你的代码的主要问题是你在加载地图之前正在绘制标记,因为它在你的函数success之外。试试这个:我已经使用静态值作为中心你可以按照它来改变它你的代码。

$(document).ready(function () {
    success(position);
});

var position = { // suppose this is your position
    coords: {
        latitude: 44.666666699999990000,
        longitude: 17.183333299999960000
    }
};

var map;
var marker, i;

var locations = [
    ['Banja Luka', 44.766666699999990000, 17.183333299999960000, 4],
    ['Tuzla', 44.532841000000000000, 18.670499999999947000, 5],
    ['Zenica', 44.203439200000000000, 17.907743200000027000, 3],
    ['Sarajevo', 43.850000000000000000, 18.250000000000000000, 2],
    ['Mostar', 43.333333300000000000, 17.799999999999954000, 1]
];

function success(position) {
    var mapcanvas = document.createElement('div');
    mapcanvas.id = 'mapcontainer';
    mapcanvas.style.height = '550px';
    mapcanvas.style.width = '960px';
    document.getElementById('article').appendChild(mapcanvas);

    var coords = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

    var options = {
        zoom: 8,
        center: coords,
        mapTypeControl: false,
        navigationControlOptions: {
            style: google.maps.NavigationControlStyle.SMALL
        },
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("mapcontainer"), options);

    marker = new google.maps.Marker({
        position: coords,
        map: map,
        title: "Vasa lokacija"
    });

    var infowindow = new google.maps.InfoWindow();

    for (i = 0; i < locations.length; i++) {
        marker = new google.maps.Marker({
            position: new google.maps.LatLng(locations[i][1], locations[i][2]),
            map: map
        });
        google.maps.event.addListener(marker, 'click', function (marker, i) {

            infowindow.setContent(locations[i][0]);
            infowindow.open(map, marker);

        });
    }
}

答案 1 :(得分:0)

你的功能的主要问题是你的标记添加循环是外部成功函数,因此它永远不会被调用。您的代码末尾还有一个额外的}。这是一个完整的解决方案jsfiddle

var map;

function success(position) {

    var mapcanvas = document.createElement('div');
    mapcanvas.id = 'mapcontainer';
    mapcanvas.style.height = '550px';
    mapcanvas.style.width = '960px';

    document.querySelector('article').appendChild(mapcanvas);

    var coords = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

    var options = {
        zoom: 15,
        center: coords,
        mapTypeControl: false,
        navigationControlOptions: {
            style: google.maps.NavigationControlStyle.SMALL
        },
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("mapcontainer"), options);

    var marker = new google.maps.Marker({
        position: coords,
        map: map,
        title: "Vasa lokacija"
    });

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(success);
    } else {
        error('Geo Location is not supported');
    }

    var locations = [
        ['Banja Luka', 44.766666699999990000, 17.183333299999960000, 4],
        ['Tuzla', 44.532841000000000000, 18.670499999999947000, 5],
        ['Zenica', 44.203439200000000000, 17.907743200000027000, 3],
        ['Sarajevo', 43.850000000000000000, 18.250000000000000000, 2],
        ['Mostar', 43.333333300000000000, 17.799999999999954000, 1]
    ];

    var infowindow = new google.maps.InfoWindow();
    var marker, i;

    for (i = 0; i < locations.length; i++) {
        marker = new google.maps.Marker({
            position: new google.maps.LatLng(locations[i][1], locations[i][2]),
            map: map
        });

        google.maps.event.addListener(marker, 'click', (function (marker, i) {
            return function () {
                infowindow.setContent(locations[i][0]);
                infowindow.open(map, marker);
            }
        })(marker, i));
    }

} //success fn ENDS

var position = {
    coords: {
        latitude: 44.766666699999990000,
        longitude: 17.183333299999960000
    }
};

success(position);