2016-04-29 9 views
-2

Ich habe eine Google Maps erstellt, die Daten aus einer CSV-Datei abrufen. Dies scheint gut zu funktionieren, aber wenn ich alle Objekte durchsuche, scheint das InfoWindow nicht zu funktionieren. Ich schätze, das liegt daran, dass die Variable innerhalb einer for-Schleife ist. Ich habe versucht, den Code in die Schleife zu verschieben, aber dieses Ergebnis in der InfoWindow platziert zufällig auf der Karte statt über die angeklickte Markierung. Wie kann ich erreichen, dass infoWindow mit mehreren Markern arbeitet?infoWindow in Google Maps auf mehreren Markierungen

db.csv Beispiel

40.740;-74.18;test;haha; 
40.740;-74.20;test;haha; 

html & java

<head> 
    <link rel="stylesheet" href="style.css"> 
    <link rel="stylesheet" href="normalize.css"> 

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> 
<script src="https://maps.googleapis.com/maps/api/js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script> 
</head> 
<body> 

<div id="mapContainer"> 

    <div id="map"></div> 
    </div> 

    <div id="abc"> 
    <div id="popupContact"> 
     <section class="register"> 
     <h1>CAMP INFORMATIONER:</h1> 
     <form method="post" action="index.html"> 

      <div class="reg_section personal_info"> 
      <input type="text" name="username" value="" placeholder="Campnavn"> 
      <textarea name="textarea" id="description" placeholder="Beskrivelse"></textarea> 
      </div> 


      <div> 
      <span class="submit" style="text-align: left; padding: 0 10px;"><input TYPE="button"name="commit" value="Tilføj" onclick="placeMarker(currentMarker, document.getElementById('description'));"></span> 
      <span class="submit" style="text-align: right; padding: 0 10px;"><input TYPE="button" name="commit" value="Fortryd" onclick="div_hide();"></span> 
      </div> 

     </form> 
     </section> 

    </div> 
    </div> 
</div> 




    <script> 
    var mapCanvas; 
    var currentMarker; 
    function initialize() { 

    var myOptions = { 
     center: {lat: 40.740, lng: -74.18}, 
     zoom : 15, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    mapCanvas = new google.maps.Map(document.getElementById("mapContainer"), myOptions); 

    var returnValue = ""; 
    var request = new XMLHttpRequest(); 

    // Read the lat/long info for markers 
    request.open("GET", "db.csv", false); 
    request.send(null); 
    returnValue = request.responseText; 

    // Convert our data from the CVS file to a usable array 
    var data = CSVToArray(returnValue); 

    for (var i = 0; i < data.length; i++) 
    { 

     // Create a lat/long object readable by Google 
     var myLatlng = new google.maps.LatLng(parseFloat(data[i][0]), parseFloat(data[i][1])); 

     // Generate a marker from the lat/long object and add it to the map 
     var marker = new google.maps.Marker({ 
     position: myLatlng, 
     map: mapCanvas, 
     title: data[i][2], 
     description: data[i][3] 

     }); 



    } 

    var imageBounds = { 
     north: 40.773941, 
     south: 40.712216, 
     east: -74.12544, 
     west: -74.22655 
    }; 
    historicalOverlay = new google.maps.GroundOverlay(
    'http://i.stack.imgur.com/0mgx2.jpg', 
    imageBounds); 
    historicalOverlay.setMap(mapCanvas); 

    // This event listener calls addMarker() when the map is clicked. 
    google.maps.event.addListener(historicalOverlay, 'click', function(e) { 
     console.log("clicked'"); 
     currentMarker = e.latLng; 
     infowindow.close(); 
     div_show(); 

    }); 


    //Changes zoom levels when the projection is available. 
    google.maps.event.addListenerOnce(mapCanvas, "projection_changed", function(){ 
     mapCanvas.setMapTypeId(google.maps.MapTypeId.HYBRID); //Changes the MapTypeId in short time. 
     setZoomLimit(mapCanvas, google.maps.MapTypeId.ROADMAP); 
     setZoomLimit(mapCanvas, google.maps.MapTypeId.HYBRID); 
     setZoomLimit(mapCanvas, google.maps.MapTypeId.SATELLITE); 
     setZoomLimit(mapCanvas, google.maps.MapTypeId.TERRAIN); 
     mapCanvas.setMapTypeId(google.maps.MapTypeId.ROADMAP); //Sets the MapTypeId to original. 
    }); 

    // InfoWindow content 
var content = '<div id="iw-container">' + 
        '<div class="iw-title">title</div>' + 
        '<div class="iw-content">' + 
        '<p>Founded in 1824, the Porcelain Factory of Vista Alegre was the first industrial unit dedicated to porcelain production in Portugal. For the foundation and success of this risky industrial development was crucial the spirit of persistence of its founder, José Ferreira Pinto Basto. Leading figure in Portuguese society of the nineteenth century farm owner, daring dealer, wisely incorporated the liberal ideas of the century, having become "the first example of free enterprise" in Portugal.</p>' + 
        '</div>' + 
        '<div class="iw-bottom-gradient"></div>' + 
       '</div>'; 

    // A new Info Window is created and set content 
    var infowindow = new google.maps.InfoWindow({ 
     content: content, 

     // Assign a maximum value for the width of the infowindow allows 
     // greater control over the various content elements 
     maxWidth: 350 
    }); 

    google.maps.event.addListener(marker, 'click', function() { 
     infowindow.open(mapCanvas, marker); 

     //title 
     document.getElementById("iw-title").innerHTML = marker.title; 

     //description 
     document.getElementById("iw-content p").innerHTML = marker.description; 

    }); 

    // Event that closes the Info Window with a click on the map 
    google.maps.event.addListener(mapCanvas, 'click', function() { 
     infowindow.close(); 
    }); 

    google.maps.event.addListener(infowindow, 'domready', function() { 

     // Reference to the DIV that wraps the bottom of infowindow 
     var iwOuter = $('.gm-style-iw'); 

     /* Since this div is in a position prior to .gm-div style-iw. 
     * We use jQuery and create a iwBackground variable, 
     * and took advantage of the existing reference .gm-style-iw for the previous div with .prev(). 
     */ 
     var iwBackground = iwOuter.prev(); 

     // Removes background shadow DIV 
     iwBackground.children(':nth-child(2)').css({'display' : 'none'}); 

     // Removes white background DIV 
     iwBackground.children(':nth-child(4)').css({'display' : 'none'}); 

     // Moves the infowindow 115px to the right. 
     iwOuter.parent().parent().css({left: '115px'}); 

     // Moves the shadow of the arrow 76px to the left margin. 
     iwBackground.children(':nth-child(1)').attr('style', function(i,s){ return s + 'left: 76px !important;'}); 

     // Moves the arrow 76px to the left margin. 
     iwBackground.children(':nth-child(3)').attr('style', function(i,s){ return s + 'left: 76px !important;'}); 

     // Changes the desired tail shadow color. 
     iwBackground.children(':nth-child(3)').find('div').children().css({'z-index' : '1'}); 

     // Reference to the div that groups the close button elements. 
     var iwCloseBtn = iwOuter.next(); 

     // Apply the desired effect to the close button 
     iwCloseBtn.css({opacity: '1', right: '38px', top: '3px', border: '7px solid #fff', 'border-radius': '13px', 'box-shadow': '0 0 5px #7D0F33'}); 

     // If the content of infowindow not exceed the set maximum height, then the gradient is removed. 
     if($('.iw-content').height() < 140){ 
     $('.iw-bottom-gradient').css({display: 'none'}); 
     } 

     // The API automatically applies 0.7 opacity to the button after the mouseout event. This function reverses this event to the desired value. 
     iwCloseBtn.mouseout(function(){ 
     $(this).css({opacity: '1'}); 
     }); 
    }); 


} 
function div_show() { 
    $('#abc').fadeIn(400); 

} 
//Function to Hide Popup 
function div_hide(){ 
    $('#abc').fadeOut(400); 
} 
    function placeMarker(location, label) { 
    var marker = new google.maps.Marker({ 
     position: location, 
     map: mapCanvas, 
     labelContent : label 
    }); 
    div_hide(); 
    } 

    function setZoomLimit(map, mapTypeId){ 
    //Gets MapTypeRegistry 
    var mapTypeRegistry = map.mapTypes; 

    //Gets the specified MapType 
    var mapType = mapTypeRegistry.get(mapTypeId); 
    //Sets limits to MapType 
    mapType.maxZoom = 15; //It doesn't work with SATELLITE and HYBRID maptypes. 
    mapType.minZoom = 15; 
    } 

    function CSVToArray(strData, strDelimiter){ 
    // Check to see if the delimiter is defined. If not, 
    // then default to comma. 
    strDelimiter = (strDelimiter || ";"); 

    // Create a regular expression to parse the CSV values. 
    var objPattern = new RegExp(
     (
      // Delimiters. 
      "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" + 

      // Quoted fields. 
      "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" + 

      // Standard fields. 
      "([^\"\\" + strDelimiter + "\\r\\n]*))" 
     ), "gi"); 

    // Create an array to hold our data. Give the array 
    // a default empty first row. 
    var arrData = [[]]; 

    // Create an array to hold our individual pattern 
    // matching groups. 
    var arrMatches = null; 

    // Keep looping over the regular expression matches 
    // until we can no longer find a match. 
    while (arrMatches = objPattern.exec(strData)) 
    { 
     // Get the delimiter that was found. 
     var strMatchedDelimiter = arrMatches[ 1 ]; 

     // Check to see if the given delimiter has a length 
     // (is not the start of string) and if it matches 
     // field delimiter. If id does not, then we know 
     // that this delimiter is a row delimiter. 
     if (strMatchedDelimiter.length && (strMatchedDelimiter != strDelimiter)) 
     { 
      // Since we have reached a new row of data, 
      // add an empty row to our data array. 
      arrData.push([]); 

     } 

     // Now that we have our delimiter out of the way, 
     // let's check to see which kind of value we 
     // captured (quoted or unquoted). 
     if (arrMatches[ 2 ]){ 
      // We found a quoted value. When we capture 
      // this value, unescape any double quotes. 
      var strMatchedValue = arrMatches[ 2 ].replace(
       new RegExp("\"\"", "g"), 
       "\"" 
       ); 

     } else 
     { 
      // We found a non-quoted value. 
      var strMatchedValue = arrMatches[ 3 ]; 
     } 

     // Now that we have our value string, let's add 
     // it to the data array. 
     arrData[ arrData.length - 1 ].push(strMatchedValue); 
    } 

    // Return the parsed data. 
    return(arrData); 
} 

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



    </script> 
    <body> 

CSS

/* Author : iMomen 
    Website: www.iMomen.com 
    E-mail : [email protected] 
*/ 


#mapContainer { 
    height: 100%; 
    width: 100%; 
    margin-left: 0px; 
    margin-right: 0px; 
    margin-bottom: 0px; 
    margin-top: 0px; 
    position: relative; 

} 

#map { 
    height: 100%; 
} 

.gm-style-mtc { 
    display: none; 
} 

.gmnoprint { 
    display: none; 
} 

#abc { 
width:100%; 
height:100%; 
top:0; 
left:0; 
display:none; 
position:fixed; 
background-color: rgba(0,0,0, .5); 
overflow:auto; 
} 

div#popupContact { 
    width: 350px; /*can be in percentage also.*/ 

    margin: 0; 
    margin: 0; 
    position: absolute; 
    top: 50%; 
    left: 50%; 
    margin-right: -50%; 
    transform: translate(-50%, -50%); 

    background-color: #ffffff; 


} 


div, span, applet, object, iframe, 
h1, h2, h3, h4, h5, h6, p, blockquote, pre, 
a, abbr, acronym, address, big, cite, code, 
del, dfn, em, img, ins, kbd, q, s, samp, 
small, strike, strong, sub, sup, tt, var, 
b, u, i, center, 
dl, dt, dd, ol, ul, li, 
fieldset, form, label, legend, 
table, caption, tbody, tfoot, thead, tr, th, td, 
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary, 
time, mark, audio, video { 
    margin: 0; 
    padding: 0; 
    border: 0; 
    font-size: 100%; 
    font: inherit; 
    vertical-align: baseline; 
} 

html, body { 
    height:100%; 
    width: 100%; 
    margin-left: 0px; 
    margin-right: 0px; 
    margin-bottom: 0px; 
    margin-top: 0px; 
    overflow:hidden; 
} 

article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section { 
    display: block; 
} 

body { 
    line-height: 1; 
} 

ol, ul { 
    list-style: none; 
} 

blockquote, q { 
    quotes: none; 
} 

blockquote:before, blockquote:after, 
q:before, q:after { 
    content: ''; 
    content: none; 
} 

table { 
    border-collapse: collapse; 
    border-spacing: 0; 
} 


a { 
    color:#FF3679; 
    text-decoration: none; 
} 
a:hover { 
    text-decoration: underline; 
} 

.container { 
    width: 350px; 
    margin-left: auto; 
    margin-right: auto; 
} 
.reg_section { 
    padding:0; 
    margin: 10px 0; 
    border-bottom: 1px dotted #eee; 
} 
.reg_section h3 { 
    font-size: 13px; 
    margin: 5px 0; 
    color: #C4A2A2; 
} 
/* Form */ 
.register { 
    text-align: center; 
    position: relative; 
    padding: 20px 20px 20px 20px; 
    background: #fff; 
    border-radius: 3px; 

} 
.register:before { 
    content: ''; 
    position: absolute; 
    top: -8px; 
    right: -8px; 
    bottom: -8px; 
    left: -8px; 
    z-index: -1; 
    background:rgba(255, 173, 200, 0.08); 
    border-radius:7px; 
    -webkit-border-radius: 7px; 
} 
.register h1 { 
    margin: -20px -20px 0; 
    line-height: 40px; 
    font-size: 15px; 
    font-weight: bold; 
    color:#694551; 
    text-align: center; 
    border-bottom:1px solid #EDEDED; 
    border-radius: 3px 3px 0 0; 
    -webkit-box-shadow: 0 1px #f5f5f5; 
    -moz-box-shadow: 0 1px #f5f5f5; 
    box-shadow: 0 1px #f5f5f5; 
} 

.register input[type=text], .register input[type=password] ,.register select,.register textarea { 
    width: 278px; 
} 
.register p.terms { 
    float: left; 
    line-height: 31px; 
} 
.register p.terms label { 
    font-size: 12px; 
    color: #777; 
    cursor: pointer; 
} 
.register p.terms input { 
    position: relative; 
    bottom: 1px; 
    margin-right: 4px; 
    vertical-align: middle; 
} 




.register-help { 
    margin: 20px 0; 
    font-size: 11px; 
    text-align: center; 

    color:#FFFFFF; 
} 
.register-help a { 
    color:#FF3679; 
    text-shadow:0 1px #1E0E13; 
} 

:-moz-placeholder { 
    color: #404040 !important; 
    font-size: 13px; 
} 

::-webkit-input-placeholder { 
    color: #ccc; 
    font-size: 13px; 
} 

input { 
    font-family:"Trebuchet MS",tahoma; 
    font-size: 14px; 
} 

input[type=text], input[type=password] ,.register select,.register textarea { 
    margin: 5px; 
    padding: 0 10px; 
    height: 34px; 
    color: #404040; 
    background: #fff; 
    border-width: 1px; 
    border-style: solid; 
    border-color: #c4c4c4 #d1d1d1 #d4d4d4; 
    border-radius:3px; 
    --webkit-border-radius: 5px; 
    outline:3px solid rgba(200, 105, 137, 0.09); 
    -moz-outline-radius:7px; 
    -webkit-box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.12); 
    -moz-box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.12); 
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.12); 
    margin:10px 0; 
} 
input[type=text]:focus, input[type=password]:focus{ 
    border-color:#FFF7F9; 
    outline-color:rgba(254, 225, 235, 0.7); 
    outline-offset: 0; 
} 

input[type=button] { 
    padding:0 0px; 
    height: 29px; 
    width: 100px; 
    font-size: 12px; 
    font-weight: bold; 
    color:#FFFFFF; 
    text-shadow:0 1px #4D1124; 
    border-width: 1px; 
    border-style: solid; 
    border-color:#693647; 
    outline: none; 
    -webkit-box-sizing: content-box; 
    -moz-box-sizing: content-box; 
    box-sizing: content-box; 
    background-color: #7D0F33; 

} 
input[type=button]:active { 
    background: #7D0F33; 
    -webkit-box-shadow: inset 0 0 3px rgba(0, 0, 0, 0.2); 
    -moz-box-shadow: inset 0 0 3px rgba(0, 0, 0, 0.2); 
    box-shadow: inset 0 0 3px rgba(0, 0, 0, 0.2); 
} 

.lt-ie9 input[type=text], .lt-ie9 input[type=password] { 
    line-height: 34px; 
} 
.register select { 
    padding:6px 10px; 
    width: 300px; 
    color: #777777; 
} 
.register textarea { 
    height: 50px; 
    padding: 10px; 
    color: #404040; 
} 

/* About */ 
.about { 
    margin:10px auto; 
    width: 300px; 
    text-align: center; 
    color:#EEA5BD; 
    font-size: 12px; 
} 
.about a { 
    padding: 1px 3px; 
    margin: 0 -1px; 
    color: #fff; 
    text-decoration: none; 
    text-shadow: 0 -1px rgba(0, 0, 0, 0.2); 
    border-radius: 2px; 
} 
.about a:hover { 
    color:#2F0916; 
    text-shadow: none; 
    background: #E83671; 
} 

.links { 
    zoom: 1; 
} 
.links:before, .links:after { 
    content: ""; 
    display: table; 
} 
.links:after { 
    clear: both; 
} 
.links a { 
    padding: 6px 0; 
    float: left; 
    width: 50%; 
    font-size: 14px; 
} 


.gm-style-iw { 
    width: 350px !important; 
    top: 15px !important; 
    left: 0px !important; 
    background-color: #fff; 
    border-radius: 2px 2px 10px 10px; 

} 
#iw-container { 
    margin-bottom: 10px; 
} 
#iw-container .iw-title { 
    font-family: 'Open Sans Condensed', sans-serif; 
    font-size: 22px; 
    font-weight: 400; 
    padding: 10px; 
    background-color: #7D0F33; 
    color: white; 
    margin: 0; 
    border-radius: 2px 2px 0 0; 
} 
#iw-container .iw-content { 
    font-size: 13px; 
    line-height: 18px; 
    font-weight: 400; 
    margin-right: 1px; 
    padding: 15px 5px 20px 15px; 
    max-height: 140px; 
    overflow-y: auto; 
    overflow-x: hidden; 
} 

.iw-subTitle { 
    font-size: 16px; 
    font-weight: 700; 
    padding: 5px 0; 
} 
.iw-bottom-gradient { 
    position: absolute; 
    width: 326px; 
    height: 25px; 
    bottom: 10px; 
    right: 18px; 
    background: linear-gradient(to bottom, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 100%); 
    background: -webkit-linear-gradient(top, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 100%); 
    background: -moz-linear-gradient(top, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 100%); 
    background: -ms-linear-gradient(top, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 100%); 
} 

Antwort

0

Ich denke, Sie sollten das Marker-Objekt in einem Array von Objekten speichern. Ex:

var arrayOfMarkers = []; 
for (var i = 0; i < data.length; i++) { 
     // Create a lat/long object readable by Google 
     var myLatlng = new google.maps.LatLng(parseFloat(data[i][0]), parseFloat(data[i][1])); 
     // Generate a marker from the lat/long object and add it to the map 
     var marker = new google.maps.Marker({ 
     position: myLatlng, 
     map: mapCanvas, 
     title: data[i][2], 
     description: data[i][3] 
     }); 
     arrayOfMarkers.push(marker); 
    } 

dann die arrayOfMarkers verwenden, um den Ereignis-Listener für jede Info Windows-

[aktualisiert - Boden-Overlay hinzufügen, nachdem Benutzer die Markierung klickt] iterieren

Bitte beachten Sie die Demo hier: http://codepen.io/dannypranoto/pen/PNdvzb

Sie müssen nur eine neue Funktion zum Erstellen eines neuen Ground Overlays mit den Parametern von imageBound

einrichten

Rufen Sie dann die Funktion innerhalb der google.maps.event.addListener(marker, ...)

this.addMarker = function(data) { 
    var Map = this; 
    var instance = this.instance; 
    var content = String.format(this.contentTemplate, data.image_url, data.name, data.summary); 
    var marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(data.latitude, data.longitude), 
     title: data.name 
    }); 
    marker.setMap(instance); 
    google.maps.event.addListener(marker, 'click', function() { 
     var imageBound = { 
      north: 40.773941, 
      south: 40.712216, 
      east: -74.12544, 
      west: -74.22655 
     } 
     Map.addGroundOverlay(imageBound); 
    }); 
} 
+0

was über diese Linie 'google.maps.event.addListener (Marker, 'klicken', function() {' Ich denke, dass dies inzwischen geändert werden soll, Kann ich nicht auf Marker zugreifen? –

+0

Ja, Sie müssen möglicherweise auf jeden Marker iterieren, indem Sie das Looping für den 'event.addListener' hinzufügen, damit es wie folgt aussieht' google.maps.event.addListener (arrayOfMarkers [i], 'click', function() {' –

+0

hmm infoWindow scheint immer noch nicht zu zeigen.ich denke ich muss' infowindow.open (mapCanvas, marker) ändern; 'to' infowindow.open (mapCanvas, arrayOfMarkers [i]); 'funktioniert aber immer noch nicht –

Verwandte Themen