2

js:

var map;

function initialize() {
  $('#refreshmap').on('click',initialize);
  var mapOptions = {
    zoom: 6,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };


  map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);

  // Try HTML5 geolocation
  if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var pos = new google.maps.LatLng(position.coords.latitude,
                                       position.coords.longitude);

      map.setCenter(pos);
    }, function() {
      handleNoGeolocation(true);
    });
  } else {
    // Browser doesn't support Geolocation
    handleNoGeolocation(false);
  }

function handleNoGeolocation(errorFlag) {
  if (errorFlag) {
    var content = 'Error: The Geolocation service failed.';
  } else {
    var content = 'Error: Your browser doesn\'t support geolocation.';
  }

  var options = {
    map: map,
    position: new google.maps.LatLng(-29.3456, 151.4346),
    content: content
  };
  var infowindow = new google.maps.InfoWindow(options);
  map.setCenter(options.position);
}

var marker = new google.maps.Marker({
    position: pos,
    title: 'Position',
    map: map,
    draggable: true,
    visible:true
  });

 updateMarkerPosition(pos);
  geocodePosition(pos); 
   google.maps.event.addListener(marker, 'drag', function() {

    updateMarkerPosition(marker.getPosition());
  });
  $('#button').click(function(){
    marker.setVisible(true);  
  });

}

google.maps.event.addDomListener(window, 'load', initialize);

html:

<div id="map-canvas"></div> 
<button  type="button" id="button" class="map_buttons button_style">Add marker</button>

上面的代码用于通过单击按钮在当前位置显示标记。地图显示当前位置但标记不起作用。

我在控制台“未捕获的 ReferenceError:pos 未定义”中收到此错误。

4

2 回答 2

2

尝试这个:

var map;
var pos;
function initialize() {
    $('#refreshmap').on('click', initialize);
    var mapOptions = {
        zoom: 6,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };


    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

    // Try HTML5 geolocation
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (position) {
            pos = new google.maps.LatLng(position.coords.latitude,
            position.coords.longitude);

            map.setCenter(pos);
        }, function () {
            handleNoGeolocation(true);
        });
    } else {
        // Browser doesn't support Geolocation
        handleNoGeolocation(false);
    }

    function handleNoGeolocation(errorFlag) {
        if (errorFlag) {
            var content = 'Error: The Geolocation service failed.';
        } else {
            var content = 'Error: Your browser doesn\'t support geolocation.';
        }

        var options = {
            map: map,
            position: new google.maps.LatLng(-29.3456, 151.4346),
            content: content
        };
        var infowindow = new google.maps.InfoWindow(options);
        map.setCenter(options.position);
    }

    var marker = new google.maps.Marker({
        position: pos,
        title: 'Position',
        map: map,
        draggable: true,
        visible: true
    });

    updateMarkerPosition(pos);
    geocodePosition(pos);
    google.maps.event.addListener(marker, 'drag', function () {

        updateMarkerPosition(marker.getPosition());
    });
    $('#button').click(function () {
        marker.setVisible(true);
    });

}

基本上,var pos在全局范围内声明,并var在初始化时删除pos

问题是

在创建标记对象期间,pos在范围内未定义

于 2013-06-06T13:43:17.397 回答
1

尝试声明 pos 全球,就像你的地图

 var map;
 var pos;
于 2013-06-06T13:42:49.870 回答