Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 向google earth添加标记_Javascript_Google Maps - Fatal编程技术网

Javascript 向google earth添加标记

Javascript 向google earth添加标记,javascript,google-maps,Javascript,Google Maps,如何向地图添加新标记?我设法显示了map函数startGoogleMaps() 但是我的函数(onclick())不起作用 function startGoogleMaps(){ var map = new google.maps.Map(document.getElementById('canvasMap'), { zoom: 5, center: initCenter, mapTypeId: google.maps.MapTyp

如何向地图添加新标记?我设法显示了map
函数startGoogleMaps()
但是我的函数(
onclick()
)不起作用

function startGoogleMaps(){    
    var map = new google.maps.Map(document.getElementById('canvasMap'), {
        zoom: 5,
        center: initCenter,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });            
}

document.getElementById("testButton").onclick = function(){
    var marker = new google.maps.Marker({
      position: (37, -97),
      map: map,
      title:"Hello World!"});
}

尝试在绑定单击事件的同一范围内定义
map
对象:

var map = null;

function startGoogleMaps(){    
    map = new google.maps.Map(document.getElementById('canvasMap'), {
        zoom: 5,
        center: initCenter,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });            
}

document.getElementById("testButton").onclick = function(){
    var marker = new google.maps.Marker({
      position: (37, -97),
      map: map,
      title:"Hello World!"});
}
还请注意,您需要将您的职位作为
google.maps.LatLng
的一个实例传递:

      ...
      position: google.maps.LatLng(37, -97),
      ...

尝试在绑定单击事件的同一范围内定义
map
对象:

var map = null;

function startGoogleMaps(){    
    map = new google.maps.Map(document.getElementById('canvasMap'), {
        zoom: 5,
        center: initCenter,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });            
}

document.getElementById("testButton").onclick = function(){
    var marker = new google.maps.Marker({
      position: (37, -97),
      map: map,
      title:"Hello World!"});
}
还请注意,您需要将您的职位作为
google.maps.LatLng
的一个实例传递:

      ...
      position: google.maps.LatLng(37, -97),
      ...

请记住,Javascript使用函数作用域。您需要像这样全局声明
map

var map;

function startGoogleMaps(){    
    map = new google.maps.Map(document.getElementById('canvasMap'), {
        zoom: 5,
        center: initCenter,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });            
}

document.getElementById("testButton").onclick = function(){
    var marker = new google.maps.Marker({
      position: new google.maps.LatLng(37, -97),
      map: map,
      title:"Hello World!"});
}
此外,标记本身可能不在地图范围内,因此您可以使用
map.fitbunds
正确显示:

document.getElementById("testButton").onclick = function(){
    var marker = new google.maps.Marker({
      position: new google.maps.LatLng(37, -97),
      map: map,
      title:"Hello World!"});

    var latlngbounds = new google.maps.LatLngBounds();
    latlngbounds.extend(new google.maps.LatLng(37, -97));
    map.fitBounds(latlngbounds);
}

请记住,Javascript使用函数作用域。您需要像这样全局声明
map

var map;

function startGoogleMaps(){    
    map = new google.maps.Map(document.getElementById('canvasMap'), {
        zoom: 5,
        center: initCenter,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });            
}

document.getElementById("testButton").onclick = function(){
    var marker = new google.maps.Marker({
      position: new google.maps.LatLng(37, -97),
      map: map,
      title:"Hello World!"});
}
此外,标记本身可能不在地图范围内,因此您可以使用
map.fitbunds
正确显示:

document.getElementById("testButton").onclick = function(){
    var marker = new google.maps.Marker({
      position: new google.maps.LatLng(37, -97),
      map: map,
      title:"Hello World!"});

    var latlngbounds = new google.maps.LatLngBounds();
    latlngbounds.extend(new google.maps.LatLng(37, -97));
    map.fitBounds(latlngbounds);
}

标记应该出现在哪里?例如,什么是position
(37,-97)
?问题是
position:(37,-97)
需要新的google.maps.LatLng(37,-97)是否使用JavaScript调试器?如果没有,请立即获取一个。标记应显示在哪里?例如,什么是position
(37,-97)
?问题是
position:(37,-97)
需要新的google.maps.LatLng(37,-97)是否使用JavaScript调试器?如果没有,请立即获取一个。当我使用th
map.fitBounds(latlngbounds)时map.fitBounds(latlngbounds)时,我的地图消失了我的地图消失