-1

我想創建一個頁面,我將與Excel與Excel集成。 。點擊時,我想添加事件監聽器到標記。我幾乎無法設法爲單個標記創建一個「單擊偵聽器」,但我無法將其用於使用循環動態創建的多個標記。創建事件監聽器來動態創建谷歌地圖標記

當點擊標記時,我想讓街景更新爲新點擊的標記位置。

我真的很感激任何建議。 謝謝。

這就是我所擁有的(來自這裏和那裏)。

<!DOCTYPE html> 
<html> 
    <head> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
    <meta charset="utf-8"> 
    <title>Simple markers</title> 
    <style> 
     html, body, #map-canvas { 
     height: 100%; 
     margin: 0; 
     padding: 0; 
     } 

    </style> 
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script> 
    <script> 


var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; //me 
var labelIndex = 0;      //me 
var map; 
var panorama; 

//this will be created from server side 

    var markers = [ 
    { lat: 39.976784, lng: -75.234347, name: "marker 1" }, 
    { lat: 39.977043, lng: -75.235087, name: "marker 2" }, 
    { lat: 39.976097, lng: -75.234508, name: "marker 3" }, 
    { lat: 39.977059, lng: -75.233682, name: "marker 4" } 
]; 
var myLatlng = new google.maps.LatLng(markers[0].lat,markers[0].lng); 


function initialize() { 
//var sv = new google.maps.StreetViewService(); 
panorama = new google.maps.StreetViewPanorama(document.getElementById('pano')); 


var mapOptions = { 
    zoom: 16, 
    center: myLatlng 
} 
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 
var panoramaOptions = { 

    navigationControl: true, 
    position: myLatlng, 
    //pov: { 
    //heading: 34, 
    //pitch: 10 
    //} 
}; 
// Set the initial Street View camera to the center of the map 
new google.maps.StreetViewPanorama(document.getElementById('pano'),  
panoramaOptions); 

//this is the loop that will creat the marker 
for (index in markers) addMarker(markers[index]); 
function addMarker(data) { 
    // Create the marker 
    var marker = new google.maps.Marker({ 
    position: new google.maps.LatLng(data.lat, data.lng), 
    map: map, 
    label: labels[labelIndex++ % labels.length], 
    title: data.name 
    }); 

} 

} 

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

</script> 
</head> 
<body> 
<div id="map-excel" style="width: 60%; height: 100%;float:left"></div> 
<div id="pano" style="width: 40%; height: 50%;float:right"></div> 
<div id="map-canvas" style="width: 40%; height: 50%;float:right"></div> 

</body> 
</html> 
+0

你要什麼標題街景點?你的標記在哪裏點擊聽衆? – geocodezip

回答

3
  1. 保持於全景的引用:
// Set the initial Street View camera to the center of the map 
new google.maps.StreetViewPanorama(document.getElementById('pano'), panoramaOptions); 

應該是:

// Set the initial Street View camera to the center of the map 
panorama = new google.maps.StreetViewPanorama(document.getElementById('pano'), panoramaOptions); 
  1. 更改點擊偵聽器中panorama的位置。
google.maps.event.addListener(marker,'click', function(e) { 
    pano.setPosition(marker.getPosition()); 
}); 

proof of concept fiddle

代碼片段:

var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; //me 
 
var labelIndex = 0; //me 
 
var map; 
 
var panorama; 
 

 
//this will be created from server side 
 

 
var markers = [{ 
 
    lat: 39.976784, 
 
    lng: -75.234347, 
 
    name: "marker 1" 
 
}, { 
 
    lat: 39.977043, 
 
    lng: -75.235087, 
 
    name: "marker 2" 
 
}, { 
 
    lat: 39.976097, 
 
    lng: -75.234508, 
 
    name: "marker 3" 
 
}, { 
 
    lat: 39.977059, 
 
    lng: -75.233682, 
 
    name: "marker 4" 
 
}]; 
 
var myLatlng = new google.maps.LatLng(markers[0].lat, markers[0].lng); 
 

 

 
function initialize() { 
 
    //var sv = new google.maps.StreetViewService(); 
 
    panorama = new google.maps.StreetViewPanorama(document.getElementById('pano')); 
 

 

 
    var mapOptions = { 
 
    zoom: 16, 
 
    center: myLatlng 
 
    } 
 
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 
 
    var panoramaOptions = { 
 

 
    navigationControl: true, 
 
    position: myLatlng, 
 
    //pov: { 
 
    //heading: 34, 
 
    //pitch: 10 
 
    //} 
 
    }; 
 
    // Set the initial Street View camera to the center of the map 
 
    pano = new google.maps.StreetViewPanorama(document.getElementById('pano'), 
 
    panoramaOptions); 
 

 
    //this is the loop that will creat the marker 
 
    for (var index in markers) addMarker(markers[index]); 
 

 
    function addMarker(data) { 
 
    // Create the marker 
 
    var marker = new google.maps.Marker({ 
 
     position: new google.maps.LatLng(data.lat, data.lng), 
 
     map: map, 
 
     label: labels[labelIndex++ % labels.length], 
 
     title: data.name 
 
    }); 
 
    google.maps.event.addListener(marker, 'click', function(e) { 
 
     pano.setPosition(marker.getPosition()); 
 
    }); 
 
    } 
 

 
} 
 

 
google.maps.event.addDomListener(window, 'load', initialize);
html, 
 
body, 
 
#map-canvas { 
 
    height: 100%; 
 
    margin: 0; 
 
    padding: 0; 
 
}
<script src="https://maps.googleapis.com/maps/api/js"></script> 
 
<div id="map-excel" style="width: 60%; height: 100%;float:left"></div> 
 
<div id="pano" style="width: 40%; height: 50%;float:right"></div> 
 
<div id="map-canvas" style="width: 40%; height: 50%;float:right"></div>

-1

//包含此代碼到你的循環

var infowindow = new google.maps.InfoWindow({ 
    content: 'info content' 
}); 
google.maps.event.addListener(marker, 'click', function() { 
    infowindow.open(map,marker); 
}); 
+0

這與我的問題沒有直接關係,但非常感謝您的快速響應。 – user3121922

0

在你這裏的功能(功能(數據){...})(數據);因爲您使用數組標記。

function addMarker(data) { 
    (function(data){ <<-- New line added 
    // Create the marker 
    var marker = new google.maps.Marker({ 
    position: new google.maps.LatLng(data.lat, data.lng), 
    map: map, 
    label: labels[labelIndex++ % labels.length], 
    title: data.name 
    }); 

    google.maps.event.addListener(marker, 'click', function(){// <<---- New line added 
     // Code event listener .... 
    });// <<---- New line added 

    })(data); // <<---- New line added 
} 

視圖多個示例這裏:https://developers.google.com/maps/documentation/javascript/examples/infowindow-simple

+0

非常感謝。你的方法也可以。但geozipcode更好,更乾淨。這是我如何拉它。 google.maps.event.addListener(標記, '點擊',函數(){ \t \t \t變種panoramaOptions = {\t \t \t \t \t位置:新google.maps.LatLng(data.lat,data.lng) \t \t \t} \t \t \t new google.maps.StreetViewPanorama(document.getElementById('pano'),panoramaOptions); – user3121922