Google maps infowindow JSON problem - json

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);
});

Related

vue and google map infowindow

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.

Google Maps V3 InfoWindow ignores latLng position

My map has no markers. In response to a click on the map, it pops up an infowindow with Lat/long shown in decimal to 5 dp and in Degrees minutes seconds. An open window is always closed prior to responding to a new click. The position is specified by position: latLng, but the infowindow is ALWAYS at the top left. I've spent days on this, and I feel I'm missing something. Code snippet below. Any ideas?
google.maps.event.addListener(map, 'click', function (event) {
var lat = event.latLng.lat(),
lng = event.latLng.lng(),
latLng = event.latLng,
strHtml;
//
// strHtml build code omitted but succesfully uses lat and lng to produce
// e.g. Latitude : 51.72229 N (51° 43' 20'' N)
// Longitude : 1.45827 E (1° 27' 30'' E)
//
// If an infowindow is open, then close it
if (openedInfoWindow != null) openedInfoWindow.close();
// latLng monitored to check it is there before use in infowindow
alert(latLng); // returns correct values which position then ignores!
var infoWindow = new google.maps.InfoWindow({
position: latLng,
content: strHtml,
maxWidth: 420
});
// Open infoWindow
infoWindow.open(map,this);
// Remember the opened window
openedInfoWindow = infoWindow;
// Close it if X box clicked
google.maps.event.addListener(infoWindow, 'closeclick', function() {
openedInfoWindow = null;
});
});
You have several problems with this code. The second parameter of the infowindow's open method has to be an MVCObject in the Core API only the Marker class can be used as an anchor. You should not need to set the infowindow variable to null and create a new infowindow object each time. You only need a single infowindow and then change its content and position. This way there is only one infowindow shown at a time.
Here is a fiddle of a working example: http://jsfiddle.net/bryan_weaver/z3Cdg/
relevant code:
google.maps.event.addListener(map, 'click', function (evt) {
var content = "<div class='infowindow'>";
content += "Latitude: " + evt.latLng.lat() + "<br/>";
content += "Longitude: " + evt.latLng.lng() + "</div>";
HandleInfoWindow(evt.latLng, content);
});
function HandleInfoWindow(latLng, content) {
infoWindow.setContent(content);
infoWindow.setPosition(latLng);
infoWindow.open(map);
}
The 2nd (optional)argument of infoWindow.open() is expected to be an MVCObject that exposes a position-property.
the this argument in the callback refers to a google.maps.Map-Instance(map), which is an MVCObject, but doesn't have a position-property.
Remove the 2nd argument from infoWindow.open(map,this); .

How to move marker on the map dynamically on click of the button

I am adding Marker dynamically and adding click event to every marker added on the map. This is the way, I am adding the marker on the map.
var pin = new google.maps.LatLng(Y, X);
var marker = new google.maps.Marker({ position: pin, map: map1 });
google.maps.event.addListener(marker, 'click', pinClicked);
marker.setMap(map1);
In the click event, based on certain conditions I am displaying a popup with certain details and buttons. One button is to close the popup and the other button is to Move marker.
What I want is when I click the Move marker button, I want the marker to be moved.
I don't want to set the draggable property to true by default, as it will be allowing the marker to be dragged when added on the map.
Marker should be draggable only when the button on the popup is clicked.
How can I get this working.
Any help is appreciated.
Thanks.
I've created this jsFiddle which shows you how to do it.
var markers = [];
function initialize() {
var myLatLng1 = new google.maps.LatLng(50.965049,5.484231);
var myLatLng2 = new google.maps.LatLng(50.97,5.474231);
var myOptions = {
zoom: 14,
center: myLatLng1,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
for (var i = 0; i < 2; i++) {
var marker = new google.maps.Marker({
position: i === 0 ? myLatLng1 : myLatLng2,
map: map,
title: 'Galaconcert ' + i,
id: i
});
markers[i] = marker;
manageBindings(marker);
}
function manageBindings(marker) {
var id = marker.id;
google.maps.event.addListener(marker, 'click', function(ev) {
var contentString =
'<div id="infowindow">' +
'Nice infowindow ' + id + '<br />' +
'<button class="move" data-id="' + id + '">move me</button> ' +
'<button class="close" data-id="' + id + '">close me</button><br />' +
'</div>'
;
marker.infowindow = new google.maps.InfoWindow();
marker.infowindow.setContent(contentString);
marker.infowindow.open(map, marker);
});
}
}
$(function(){
$('button.move').live('click', function(){
var id = $(this).data('id');
markers[id].setDraggable(true);
});
$('button.close').live('click', function(){
var id = $(this).data('id');
markers[id].infowindow.close();
});
initialize();
});
You should keep all your created markers in an array so you can access them. In an infowindow you store the reference to that array - in my example it's in button's data attribute. Then when anything happens in infowindow, you are able to access the particular marker and set it's draggable option to true.
The code should be refactored and should not use live but you should have the idea.
There is a method for Markers that allows you to set their draggable option.
Then also change the button to say place marker so that when you've dragged the marker to where you want it to be you can click it again to place the marker and set the draggable property to false again.
new google.maps.event.addListener(button, 'click', function(){
if(button.status == "Move"){
marker.setDraggable(true);
} else {
marker.setDraggable(false);
}
});
Please note this is psuedo code just highlighting the setDraggable function.
Google markers have property setDraggable by which it can be set marker draggable to true or false.
marker.setDraggable(true); or
marker.setDraggable(false);

Google API and map.panTo: not panning

I'm trying to improve my sound project site, but I'm stuck in... panning.
I have a Google map populated by markers, associated to a text and a recorded sound. It works. I'm trying to allow user to navigate map through panning, by I can't get my map panning.
I'm not an expert in php or Google API and this is my first post here, hope it's well formatted.
The web address of the project is soundplaces.net
I've created a function map_panning(), and then tried to call this function with
please PAN
in content div.
The map just reloads... even if I tried to place a "return false" in the function.
I also tried to make the map pan when a marker is clicked
GEvent.addListener(marker, 'click', function() {
marker.openInfoWindowHtml(html); map.panTo(marker.getPoint());
});
But still, does not work.
Can anybody help me?
This a greater part of the code I have in my default.ctp (the site is made with CAKE), hope it helps.
function load() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng(<?php echo $CenterMap; ?>), 13);
map.setMapType(G_SATELLITE_MAP);
GDownloadUrl("http://www.soundplaces.net/xml.php", function(data) {
var xml = GXml.parse(data);
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("name");
var description = markers[i].getAttribute("description");
var address = markers[i].getAttribute("address");
var type = markers[i].getAttribute("type");
var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var file_name = markers[i].getAttribute("file_name");
var marker = createMarker(point, name, description, address, type, file_name);
map.addOverlay(marker);
}
});
}
}
function createMarker(point, name, description, address, type, file_name) {
//var marker = new GMarker(point, customIcons[type]);
markerOptions = { icon:soundIcon };
var marker = new GMarker(point, markerOptions);
var html = "<b>" + name + "</b> <br/>" + address + "<br />" + description + "<br />" + "<object type='application/x-shockwave-flash' data='/media/dewplayer.swf?mp3=/media/audio/"+file_name+"' width='200' height='20'><param name='movie' value='dewplayer.swf?mp3=media/audio/"+file_name+"' /></object>";
GEvent.addListener(marker, 'click', function() {
marker.openInfoWindowHtml(html); map.panTo(marker.getPoint());
});
return marker;
}
function map_panning() {
map.panTo(new google.maps.LatLng(44.433373,10.712251));
return false; //cancel navigation
}
Then in body
<body onload="load()" onunload="GUnload()">
<div id="content">
Please PAN
<?php echo $this->Session->flash(); ?>
<?php echo $content_for_layout; ?>
</div>
<?php echo $this->element('sql_dump'); ?>
</body>
You're mixing up a combination of Google Maps API 2 and API 3 code. The two don't work together, you have to use one or the other. API 2 is deprecated, so you should preferably use API 3.
e.g. new google.maps.LatLng(44.433373,10.712251) is API 3 but new GMarker(point, markerOptions) is API 2.
And if you did want to keep it all with API 2, you should probably amend your map_panning function to be something like:
map.panTo(new GLatLng(44.433373,10.712251));

Google Maps Api adding label

i'm using the the following function to create my markers from a XML file. i wish to label each market 1,2,3,4,5,6 etc where 'i' is the number. can some please please tell me how to incorporate this. thank you
function createMarker(point, name, address, type, i) {
var marker = new GMarker(point, customIcons[type]);
var html = "<b>" + name + "</b> <br/>" + address;
GEvent.addListener(marker, 'click', function() {
marker.openInfoWindowHtml(html);
});
return marker;
}
You can use Google's Chart server to create dynamic icons, and set that as your marker image.
Chart server docs on using dynamic icons: http://code.google.com/apis/chart/docs/gallery/dynamic_icons.html
For example,
http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=1|FF0000|000000 will render as
and http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=2|FF0000|000000 will get you
You can make your own Marker icons, and implement them onto your map, see here
(It might be best to use the current Google Maps API v3 if you aren't already)
Do something like:
function createMarker(point, name, address, i) {
var image = "icon" + i + ".png";
var html = "<b>" + name + "</b> <br/>" + address;
var marker = new google.maps.Marker({
position: point,
map: map,
icon: image,
title: name
});
addinfowindow(marker, html);
return marker;
}
(where you have made an icon for each marker called icon1.png to icon6.png and placed in the directory)
Also for multiple info windows you may need to create a new global function addinfowindow() with a infowindow defined globally (see here).
function addwindow(pmarker, phtml){
google.maps.event.addListener(pmarker, 'click', function() {
infowindow.setContent(phtml);
infowindow.open(map, pmarker);
});
}