简体   繁体   English

添加标记-Google Maps API v3

[英]Add Marker - Google Maps API v3

I have the following Javascript where I modified the standard Google Maps API initialize() function and the standard addMarker function. 我使用以下Javascript修改了标准Google Maps API的initialize()函数和标准的addMarker函数。 The map loads and centers fine, but the marker does not get added to the map. 地图可以很好地加载和居中,但不会将标记添加到地图中。 I looked at the previous question , but I'm not sure what I'm doing wrong.. 我看了前面的问题 ,但不确定自己在做什么错。

<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>

So your problem is to do with scope: 因此,您的问题与范围有关:

Add this above your initilize function: 将此添加到您的初始化函数上方:

var map;

And in your initialize function change this: 并在您的初始化函数中更改此:

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

to this: 对此:

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

You don't need to re-create the map in your getlocation function: 您无需在getlocation函数中重新创建地图:

so remove these lines: 因此删除这些行:

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

Here is a jsfiddle with the changes: 这是一个包含更改的jsfiddle:

http://jsfiddle.net/bQ3AG/ http://jsfiddle.net/bQ3AG/

EDIT: I've just noticed when you pin point their location, you want to set the zoom level to 10. 编辑:我刚刚注意到,当您确定其位置时,您要将缩放级别设置为10。

To do this you can use: map.setZoom(10); 为此,您可以使用: map.setZoom(10); instead of passing through new options. 而不是通过新的选项。

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

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