添加标记 - Google Maps API v3

时间:2011-11-30 00:43:32

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

我有以下Javascript,我修改了标准的Google Maps API initialize()函数和标准的addMarker函数。地图加载和居中很好,但标记不会添加到地图中。我看了previous question,但我不确定我做错了什么..

<script type="text/javascript">
// Note that using Google Gears requires loading the Javascript
// at http://code.google.com/apis/gears/gears_init.js

var siberia = new google.maps.LatLng(60, 105);
var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687); 
var usa = new google.maps.LatLng(44.58, -95.46); //middle of the US w 50 states     

var browserSupportFlag =  new Boolean();

function initialize() {
    var myOptions = {
    zoom: 4,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions)
  map.setCenter(usa);   
}

function getMyLocation() {
    var initialLocation= newyork;
    var myOptions = {
    zoom: 10,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    // Try W3C Geolocation (Preferred)
    if(navigator.geolocation) {
        browserSupportFlag = true;
        navigator.geolocation.getCurrentPosition(function(position) {
        initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
        map.setCenter(initialLocation);
        addMarker(initialLocation);
    }, function() {
    handleNoGeolocation(browserSupportFlag);
    });
    // Try Google Gears Geolocation
    } else if (google.gears) {
        browserSupportFlag = true;
        var geo = google.gears.factory.create('beta.geolocation');
        geo.getCurrentPosition(function(position) {
        initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
        map.setCenter(initialLocation);
        addMarker(initialLocation);
    }, function() {
        handleNoGeoLocation(browserSupportFlag);
    });
    // Browser doesn't support Geolocation
    } else {
        browserSupportFlag = false;
        handleNoGeolocation(browserSupportFlag);
    }

    function handleNoGeolocation(errorFlag) {
        if (errorFlag == true) {
            alert("Geolocation service failed.");
            initialLocation = newyork;
        } else {
             alert("Your browser doesn't support geolocation. We've placed you in Siberia.");
             initialLocation = siberia;
        }
        map.setCenter(usa);
    }

}

function addMarker(location) {
    marker = new google.maps.Marker({
    position: location,
    map: map, 
    title: "Hello World!"
    });
    //markersArray.push(marker);
}
</script>

1 个答案:

答案 0 :(得分:0)

所以你的问题与范围有关:

在initilize函数上方添加:

var map;

在你的初始化函数中改变这个:

  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions)

到此:

  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions)

您无需在getlocation函数中重新创建地图:

删除这些行:

   var myOptions = {
    zoom: 10,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

这是一个有变化的jsfiddle:

http://jsfiddle.net/bQ3AG/

编辑:我刚注意到当你指向他们的位置时,你想将缩放级别设置为10。

要执行此操作,您可以使用:map.setZoom(10);而不是通过新选项。