I am using vue and google map, and I want a real time experience by updating the data displayed in the info window. I have tried following:
var infoStr =
'<table class="">'+
'<tbody>' +
'<tr>' +
'<td colspan="2">{{testInfo}}</td>' +
'</tr>' +
'</tbody>'+
'</table >';
var infowindow = new google.maps.InfoWindow({
content: infoStr
});
global.infoWindow = infowindow;
infowindow.open(map, marker);
The application displays {{testInfo}} like it was a string and is not bound. Is there anyway to bind data in the InfoWindow so I can bind it to Vue data?
Your problem is that when you put {{testInfo}} into the variable infoStr you are making it static, therefore it will only have value of {{testinfo}} at the time the infowindow is created.
As a workaround I suggest that you add id="info-win-1" attribute to your infowindow code.
var infoStr =
'<table class="">'+
'<tbody>' +
'<tr>' +
'<td id="info-win-1" colspan="2">{{testInfo}}</td>' +
'</tr>' +
'</tbody>'+
'</table >';
var infowindow = new google.maps.InfoWindow({
content: infoStr
});
That way you can dynamically change the content using document.getElementById("info-win-1").innerHTML = testInfo from your vue app.
Related
I use the Google Maps & Google Places API the show my business data on a map. Actually the info window opens when i click the map marker but my goal is, that the infowindow opens on page load.
var infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.getDetails(request, function(place, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent('<div><strong>' + place.name + '</strong><br>' +
/* place.formatted_address + '<br />' + */
place.address_components[1].long_name + ' ' + place.address_components[0].long_name + '<br />' +
place.address_components[6].long_name + ' ' + place.address_components[2].long_name + '<br />' +
place.rating + place.user_ratings_total + '</div>');
infowindow.open(map, this);
});
}
});
You should move the code for info window and marker in main area
var infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
infowindow.setContent('<div><strong>' + place.name + '</strong><br>' +
/* place.formatted_address + '<br />' + */
place.address_components[1].long_name + ' ' + place.address_components[0].long_name + '<br />' +
place.address_components[6].long_name + ' ' + place.address_components[2].long_name + '<br />' +
place.rating + place.user_ratings_total + '</div>');
infowindow.open(map, marker);
Let me explain the project a bit. i have a huge list of stores with addresses (longitude and latitude and code client ....).
Now, my problem is, i must be able to filter these markers depending on CodeClient i mean to find the client in google maps based on CodeClient .
The implementation is similar to the answer I mentioned before. You should add an input for code of client and a search button. In the function where you create markers add a property 'code' for each marker marker.code = CodeClient;. When you click search button it executes the filterByCode() function. If you pass empty value it restores all markers.
Have a look at modified code
// necessary variables
var map;
var infoWindow;
var markersData = [];
var markerCluster;
var markerArray = []; //create a global array to store markers
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(32, -6),
zoom: 7,
mapTypeId: 'roadmap',
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
// create MarkerClusterer, markersArray is not populated yet
markerCluster = new MarkerClusterer(map, [], {
imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
});
// a new Info Window is created
infoWindow = new google.maps.InfoWindow();
// Event that closes the Info Window with a click on the map
google.maps.event.addListener(map, 'click', function() {
infoWindow.close();
});
// Finally displayMarkers() function is called to begin the markers creation
displayMarkers();
}
google.maps.event.addDomListener(window, 'load', initialize);
// This function will iterate over markersData array
// creating markers with createMarker function
function displayMarkers() {
// this variable sets the map bounds according to markers position
var bounds = new google.maps.LatLngBounds();
// for loop traverses markersData array calling createMarker function for each marker
$.get("https://gist.githubusercontent.com/abdelhakimsalama/358669eda44d8d221bf58c629402c40b/raw/bae93c852669a35f0e7053e9f8d068841ddf146a/get_data_google_api", function(response) {
markersData = JSON.parse(response);
for (var i = 0; i < markersData.length; i++) {
var latlng = new google.maps.LatLng(markersData[i].Latitude, markersData[i].Longitude);
var Route = markersData[i].Route;
var Secteur = markersData[i].Secteur;
var Agence = markersData[i].Agence;
var CodeClient = markersData[i].CodeClient;
var PrecisionGPS = markersData[i].PrecisionGPS;
var Latitude = markersData[i].Latitude;
var Longitude = markersData[i].Longitude;
var BarCode = markersData[i].BarCode;
var PrenomClient = markersData[i].PrenomClient;
var NumAdresse = markersData[i].NumAdresse;
var Tel = markersData[i].Tel;
var Whatsapp = markersData[i].Whatsapp;
var NbrFrigo = markersData[i].NbrFrigo;
var OpenAm = markersData[i].OpenAm;
var CloseAm = markersData[i].CloseAm;
var OpenPm = markersData[i].OpenPm;
var ClosePm = markersData[i].ClosePm;
var OpenAmVen = markersData[i].OpenAmVen;
var CloseAmVen = markersData[i].CloseAmVen;
var OpenPmVen = markersData[i].OpenPmVen;
var ClosePmVen = markersData[i].ClosePmVen;
var OpenAmDim = markersData[i].OpenAmDim;
var CloseAmDim = markersData[i].CloseAmDim;
var OpenPmDim = markersData[i].OpenPmDim;
var ClosePmDim = markersData[i].ClosePmDim;
var IMEI = markersData[i].IMEI;
var Date_Added = markersData[i].Date_Added;
createMarker(latlng, Route, Agence, Secteur, CodeClient, PrecisionGPS, Latitude, Longitude, BarCode, PrenomClient, NumAdresse, Tel, Whatsapp, NbrFrigo, OpenAm, CloseAm, OpenPm, ClosePm, OpenAmVen, CloseAmVen, OpenPmVen, ClosePmVen, OpenAmDim, CloseAmDim, OpenPmDim, ClosePmDim, IMEI, Date_Added);
// marker position is added to bounds variable
bounds.extend(latlng);
}
// Finally the bounds variable is used to set the map bounds
// with fitBounds() function
map.fitBounds(bounds);
});
}
// This function creates each marker and it sets their Info Window content
function createMarker(latlng, Route, Agence, Secteur, CodeClient, PrecisionGPS, Latitude, Longitude, BarCode, PrenomClient, NumAdresse, Tel, Whatsapp, NbrFrigo, OpenAm, CloseAm, OpenPm, ClosePm, OpenAmVen, CloseAmVen, OpenPmVen, ClosePmVen, OpenAmDim, CloseAmDim, OpenPmDim, ClosePmDim, IMEI, Date_Added) {
var marker = new google.maps.Marker({
map: map,
position: latlng,
title: CodeClient
});
marker.code = CodeClient;
markerArray.push(marker); //push local var marker into global array
// add marker to the MarkerClusterer
markerCluster.addMarker(marker);
// This event expects a click on a marker
// When this event is fired the Info Window content is created
// and the Info Window is opened.
google.maps.event.addListener(marker, 'click', function() {
var dicoFrigosDetails = {};
var HTMLtext = "";
for (var i = 1; i <= Object.keys(dicoFrigosDetails).length / 4; i++) {
HTMLtext += '';
}
// Creating the content to be inserted in the infowindow
var iwContent = '<div id="iw_container">' +
'<div class="iw_title">Code Client : ' + CodeClient +
'</div>' + '<div class="iw_content">Précision : ' + PrecisionGPS +
'<br />Latitude : ' + Latitude +
'<br />Longitude : ' + Longitude +
'<br />Route : ' + Route +
'<br />Secteur : ' + Secteur +
'<br />Agence : ' + Agence +
'<br />Code-barres : ' + BarCode +
'<br />prenom de Client : ' + PrenomClient +
//'<br />nom Complet de Client : ' + PrenomClient + ' ' + NomClient +
'<br />Num Adresse : ' + NumAdresse +
'<br />Téléphone : ' + Tel +
'<br />Whatsapp : ' + Whatsapp +
'<br />Nbr Frigos : ' + NbrFrigo + HTMLtext +
'<br />Ouverture Matin : ' + OpenAm +
'<br />Fermeture Matin : ' + CloseAm +
'<br />Ouverture après-midi : ' + OpenPm +
'<br />Fermeture Après-midi : ' + ClosePm +
'<br />Ouverture Matin Ven : ' + OpenAmVen +
'<br />Fermeture Matin Ven : ' + CloseAmVen +
'<br />Ouverture après-midi Ven: ' + OpenPmVen +
'<br />Fermeture après-midi Ven: ' + ClosePmVen +
'<br />Ouverture Matin Dim : ' + OpenAmDim +
'<br />Fermeture Matin Dim : ' + CloseAmDim +
'<br />Ouverture après-midi Dim : ' + OpenPmDim +
'<br />Fermeture après-midi Dim : ' + ClosePmDim +
'<br />IMEI : ' + IMEI +
'<br />Date Passage : ' + Date_Added +
'</div>';
// including content to the Info Window.
infoWindow.setContent(iwContent);
// opening the Info Window in the current map and at the current marker location.
infoWindow.open(map, marker);
});
}
function filterByCode() {
var code = document.getElementById("code-client").value;
var bounds = new google.maps.LatLngBounds();
markerCluster.clearMarkers();
if (code) {
markerArray.forEach(function(marker) {
marker.setMap(null);
});
var filtered = markerArray.filter(function(marker) {
return marker.code === code;
});
if (filtered && filtered.length) {
filtered.forEach(function(marker) {
bounds.extend(marker.getPosition());
marker.setMap(map);
});
markerCluster.addMarkers(filtered);
markerCluster.redraw();
map.fitBounds(bounds);
}
} else {
markerArray.forEach(function(marker) {
bounds.extend(marker.getPosition());
marker.setMap(map);
});
markerCluster.addMarkers(markerArray);
markerCluster.redraw();
map.fitBounds(bounds);
}
}
html {
height: 100%;
background: gray;
}
body {
height: 100%;
margin: 0;
padding: 0;
}
#map-canvas {
height: 100%;
}
#iw_container .iw_title {
font-size: 16px;
font-weight: bold;
}
.iw_content {
padding: 15px 15px 15px 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<title>InnoTech - Map - Server</title>
<meta charset="utf-8">
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js"></script>
<script type="text/javascript" src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script>
<div id="search-code-container">
<input id="code-client" title="Enter code" type="text" placeholder="Enter code (E.g. 511557)" value=""/>
<input id="code-client-btn" type="button" value="Search" onclick="filterByCode()" />
</div>
<div id="map-canvas" />
I hope this helps!
I have an html checkbox that runs an if else javascript function depending on whether it is check or unchecked. When the checkbox is checked it loads the markers called "Fishmark" from the DB. This works fine.
What I need help with is getting the markers to remove when the box is unchecked. I know the if, else statement is correct so it has be with the actual Google API code. I successfully used the setMap(null) on another part of my site so I'm kinda lost as to why its not taking all of the Fishmark markers off the map.
var checkbox = document.getElementById("chbx");
function addRemoveFishing(){
if(checkbox.checked==true){
fishingUrl("XML_Fishing.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var title = markers[i].getAttribute("title");
var water_type = markers[i].getAttribute("water_type");
var water_depth = markers[i].getAttribute("water_depth");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var bait = markers[i].getAttribute("bait");
var fish = markers[i].getAttribute("fish");
var notes = markers[i].getAttribute("notes");
var Fishhtml = "<b>" + title + "</b> <br/>" + water_type + "</b> <br/>" + water_depth + "</b> <br/>" + bait
+ "</b> <br/>" + fish + "</b> <br/>" + notes;
var icon = FishingIcon;
var Fishmark = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon
});
bindInfoWindow4(Fishmark, map, Fishinfo, Fishhtml);
}
});
}
else {
Fishmark.setMap(null);
}
};
You are creating several markers in a for loop. In your else statement you are only removing one (1) marker from the map.
Store the markers in an array for instance and in your else statement make a for loop that calls setMap(null) for each marker.
I have recently set up a mobile version of my site and am having a little trouble with the Google Maps V3 infowindows on the mobile version.
I am trying to add some data from my JSON file to appear in the infowindows. I am totally new to JSON and javascript as well really.
Here is the content string that I have for the infowindows on my main desktop site which is working perfectly.
var contentString = '<div align="left"><p style="color:#000000;"><a href="http://publicaccessgolf.com.au/' + data.course[i].slug + '" >' + data.course[i].name + '</a><br />' + data.course[i].address + '<br />' + data.course[i].contact + '</p></div>';
I want to add this to my mobile site but it will not work, the map either doesn’t display at all or there are no markers displayed.
Here is part of the code for my mobile site
function addMarker(map, position){
var marker = $('#map_canvas').gmap('addMarker', { 'position': position });
var contentString = 'Need to add info in here...' ;
var infoWindow = $('#map_canvas').gmap('addInfoWindow', { 'position':marker.get(0).getPosition(), 'content': contentString });
marker.click(function() {
infoWindow.get(0).open(map, marker.get(0));
map.panTo(marker.get(0).getPosition());
});
}
Any ideas why the code from the normal site would not be working??
Could there be problems with the content, e.g. an apostrophe in data.course[i].name?
Have you tried just doing this (to replace your marker.click() function):
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(contentString);
infoWindow.open(map, this);
map.panTo(marker.get(0).getPosition());
});
You can access the data of a google.maps.data layer added as geoJson just by calling event.feature.getProperty('nameofproperty'). Where 'nameofproperty' could be numer or string (the name of property in your geoJson-file).
Here a simple example I'm using that works fine:
//Load mapdata via geoJson
myDataLayer = new google.maps.Data();
myDataLayer.loadGeoJson("myData.geojson");
var featureStyle = {
icon: "mm_purple.png"
};
myDataLayer.setStyle(featureStyle);
myDataLayer.setMap(map);
//listen for click events and show infowindow
var infoWindow = new google.maps.InfoWindow();
myDataLayer.addListener('click', function(event) {
infoWindow.setContent('first property: '+ event.feature.getProperty('myfirstproperty') +'<br /> second proporty: '+ event.feature.getProperty('mysecondproperty') +
'<br /> last property: '+ event.feature.getProperty('mylastproperty'));
infoWindow.setPosition(event.latLng);
infoWindow.open(map);
});
i've started very enthousiastic with google maps and managed to get the info from my mysql dbase to the map on my site. The problem i'm facing now is that i want to be able to click through from the info window to a page on my site (the link also comes out of the dbase).
Here is the standard google api code I'm using now ('mijnlink' is the link), i've tried a lot of different things but so far no solution, hope you can help me to find the answer:
GDownloadUrl("../phpsqlajax_genxml.php", function(data) {
var xml = GXml.parse(data);
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var mijnlink = markers[i].getAttribute("mijnlink");
var naam = markers[i].getAttribute("naam");
var address = markers[i].getAttribute("address");
var hotelcampingbungalow = markers[i].getAttribute("hotelcampingbungalow");
var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var marker = createMarker(point, mijnlink, naam, address, hotelcampingbungalow);
map.addOverlay(marker);
}
});
function createMarker(point, mijnlink, naam, address, hotelcampingbungalow) {
var marker = new GMarker(point, customIcons[hotelcampingbungalow]);
var html = "<b>" + mijnlink + hotelcampingbungalow + " " + naam + "</b> <br/>" + address;
GEvent.addListener(marker, 'click', function() {
marker.openInfoWindowHtml(html);
});
return marker;
}
Got some other help, the var html in the Createmarker function must be like this:
var html = "<b><a href='" + mijnlink + "'>klik hier</a>" + hotelcampingbungalow + " " + naam + "</b> <br/>" + address;