I am using Gmap3 jQuery plugin with infobubble plugin and php to get json reponse for adding markers.
I have added GoogleMap marker using addMarkers option of Gmap3 plugin.
{
action: 'addMarkers',
markers:address_data,
marker:
{
options:
{
draggable: false,
icon: HOST+'img/icons/google_marker.png',
animation: google.maps.Animation.DROP
},
events:
{
click: function(marker, event, data)
{
var map = $(this).gmap3('get');
infoBubble = new InfoBubble({
maxWidth: 310,
shadowStyle: 1,
padding: 5,
borderRadius: 10,
arrowSize: 20,
borderWidth: 5,
borderColor: '#CCC',
disableAutoPan: true,
hideCloseButton: false,
arrowPosition: 50,
arrowStyle: 0
});
if (!infoBubble.isOpen())
{
infoBubble.setContent(data);
infoBubble.open(map, marker);
console.log('open');
}
else
{
infoBubble.close();
}
}
}
}
}
All is working well on first attempt but when i click on marker then infobubble keeps popping up.
Means if i have one marker and some content to display in bubble then when i keep clicking on same marker infobubble added one on other but what i need "I need to close old infobubble if marker clicked again or other marker is clicked " just like normal infowindow dose.
Hope i can make clear point.
Thanks.
Declare infoBubble as a var outside the click handler, and instantiate it there.
Then the checks for infoBubble.isOpen() will be relevant.
From the code you provided, you create a new infoBubble on each click, hence the infoBubble.isOpen() check applies to that newly created object.
How to do it
Declare var infobubble; as global variable.
and inside of click event handler add below line that will do that.
if( infoBubble != null ) { infoBubble.close(); }
so code will look as below,
var infobubble;
//other code for getting markers and all and then `addMarkers` code
{
action: 'addMarkers',
markers:address_data,
marker:
{
options:
{
draggable: false,
icon: HOST+'img/icons/google_marker.png',
animation: google.maps.Animation.DROP
},
events:
{
click: function(marker, event, data)
{
var map = $(this).gmap3('get');
if( infoBubble != null ) { infoBubble.close(); }
infoBubble = new InfoBubble({
maxWidth: 310,
shadowStyle: 1,
padding: 5,
borderRadius: 10,
arrowSize: 20,
borderWidth: 5,
borderColor: '#CCC',
disableAutoPan: true,
hideCloseButton: false,
arrowPosition: 50,
arrowStyle: 0
});
infoBubble.setContent(data);
infoBubble.open(map, marker);
}
}
}
}
Related
I want to set a different color marker depending on the type that it gets from my database. For example the type is FIRE and I want the marker to be color red. Is it possible?
Here is my maps.html
(function(){
var map,marker,latlng,bounds,infowin;
/* initial locations for map */
var _lat=14.676;
var _lng=121.0437;
function showMap(){
/* set the default initial location */
latlng={ lat: _lat, lng: _lng };
bounds = new google.maps.LatLngBounds();
infowin = new google.maps.InfoWindow();
/* invoke the map */
map = new google.maps.Map( document.getElementById('map'), {
center:latlng,
zoom: 10
});
/* show the initial marker */
marker = new google.maps.Marker({
position:latlng,
map: map,
title: 'Hello World!'
});
bounds.extend( marker.position );
/* jQuery */
$.ajax({
url: 'get.php',
data: {'ajax':true },
dataType: 'json',
success: function( data, status ){
$.each( data, function( i,item ){
/* add a marker for each location in response data */
addMarker( item.lat, item.lng, item.username);
});
},
error: function(){
output.text('There was an error loading the data.');
}
});
}
/* simple function just to add a new marker */
function addMarker(lat,lng,title){
marker = new google.maps.Marker({/* Cast the returned data as floats using parseFloat() */
position:{ lat:parseFloat( lat ), lng:parseFloat( lng ) },
map:map,
title:title
});
google.maps.event.addListener( marker, 'click', function(event){
infowin.setContent(this.title);
infowin.open(map,this);
infowin.setPosition(this.position);
}.bind( marker ));
bounds.extend( marker.position );
map.fitBounds( bounds );
}
document.addEventListener( 'DOMContentLoaded', showMap, false );
}());
</script>
<style>
html, html body, #map{ height:100%; width:100%; padding:0; margin:0; }
</style>
</head>
<body>
<div id='map'></div>
</body>
Here is my get.php where I get the data from the database.
$mysql ="SELECT lat,lng,username,type FROM `tbl_coordinates`";
$result = mysqli_query($connect, $mysql);
if (!empty($result))
{
while ($row=mysqli_fetch_array($result))
{
$latlng[] = array(
'lat' => $row['lat'],
'lng' => $row['lng'],
'username' => $row['username'],
'type' => $row['type'],
);
}
}
mysqli_close($connect);
header('Content-type:application/json;charset=utf-8');
echo json_encode($latlng);
?>
You have to add one more parameter icon in below functions,
/* show the initial marker */
marker = new google.maps.Marker({
position:latlng,
map: map,
title: 'Hello World!',
icon: 'marker1.png' // path of your icon image.
});
And visit this link :- http://www.benjaminkeen.com/google-maps-coloured-markers/ from this link you can download all the markers. So put this marker's image in your directory and give path to that image in icon parameter.
This is the easiest way of adding other marker in google map.
EDIT :
So for that you have to put condition's like if it's color is red then put red icon. if flood than flood's icon.
if you got value in $color = 'red'; from php
than in javascript put it like this,
var color = <?php echo $color; ?>
then check using color that,
if(color == 'red')
{
icon: 'red.png'
}
if(color == 'blue')
{
icon: 'flood.jpg'
}
Hi i have an web app where i integrate gmap on it which will look similar to Yelp gmap services. The issues is that when i mouseover the marker the infobox stuck within the box as follow:
the infobox suppose to look like this
Below are my current code for integrate gmap
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/src/infobox_packed.js"></script>
<script type="text/javascript">
var gmap, gpoints = [];
$(document).ready(function() {
initialize();
});
function initialize() {
gmap = new google.maps.Map(document.getElementById('themap'), {
zoom: 8,
streetViewControl: false,
scaleControl: false,
center: new google.maps.LatLng(3.3000000,101.9629796),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
<?php
$content = '<p>Test</p>';
?>
gpoints.push( new point(gmap, '<?php echo $lat; ?>', '<?php echo $long; ?>', '<?php echo $content; ?>') );
}
function point(_map, lat, lng, content) {
this.marker = new google.maps.Marker({
position: new google.maps.LatLng(lat,lng),
map: _map,
zIndex: 11
});
this.content = content;
var gpoint = this;
google.maps.event.addListener(gpoint.marker, 'mouseover', function() {
popup(gpoint);
});
google.maps.event.addListener(_map, 'zoom_changed', function() {
center=gpoint.marker.getPosition();
lat=center.lat();
lng=center.lng();
// alert(lat+"===="+lng);
});
}
function popup(_point) {
_point.popup = new InfoBox({
content: _point.content,
position: _point.marker.getPosition(),
// shadowStyle: 1,
padding: 20,
//backgroundColor: '#ddd',
borderRadius: 10,
arrowSize: 10,
borderWidth: 1,
// borderColor: '#dddddd',
disableAutoPan: true,
arrowPosition: 30,
// backgroundClassName: 'phoney',
arrowStyle: 2
});
_point.popup.open(_point.marker.map, _point.marker);
google.maps.event.addListener(_point.popup, 'domready', function() {
google.maps.event.addDomListener(_point.marker, 'mouseout', function() {
_point.popup.close();
});
});
}
</script>
You may use the option pixelOffset of the infoBox to control the position related to the marker.
The default is 0,0 , which means that the top left corner of the infoBox sticks on the position(of the marker).
To place it top+center, you must explicitly assign to the content of the infobox, and apply a pixelOffset as follows:
pixelOffset:new google.maps.Size(-halfOfWidthOfTheBox,0)
When you don't know the size of the infoBox you may calculate it.
Add this to the domready-callback of _point.popup:
this.setOptions({pixelOffset:new google.maps
.Size(-parseInt(this.div_.offsetWidth/2,10),0)});
I've got a Google Maps where users can click on the map to insert a marker with an infowindow form where they can edit and save data to the database. Its similar to this:
http://code.google.com/apis/maps/articles/phpsqlinfo_v3.html
but I've made my marker to be draggable. How do I pass the updated lat & lng to another PHP page to update the database?
Currently, I'm doing this:
function updateMarkerPosition(latLng)
{
document.getElementById('add_lat').value = latLng.lat();
document.getElementById('add_lng').value = latLng.lng();
}
function add_editable_mkr()
{
google.maps.event.addListener(map, 'click', function(event)
{
var marker_drag = new google.maps.Marker(
{
position: event.latLng,
draggable:true,
map: map,
icon: icon2
});
// Infowindow form
var html = '<div>'+
'<form name="add_data" action="phpinsert.php">'+
'<input type="text" name="add_lat" value=""/>'+
'<input type="text" name="add_lng" value=""/>'+
'</form>'+
'</div>';
var popup_form = new google.maps.InfoWindow
({
maxWidth:800
});
google.maps.event.addListener(marker_drag, "click", function()
{
popup_form.setContent(html);
popup_form.open(map, marker_drag);
});
google.maps.event.addListener(marker_drag, 'drag', function()
{
updateMarkerPosition(marker_drag.getPosition());
});
});
}
function initialize()
{
map = new google.maps.Map(document.getElementById("map"),
{
center: new google.maps.LatLng(1.364236,103.796082),
zoom: 4,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: true,
mapTypeControlOptions:
{
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
},
navigationControl: true,
navigationControlOptions:
{
style: google.maps.NavigationControlStyle.LARGE
}
});
add_editable_mkr();
}
But the values are passed over to my other page as empty values. Is there any other way to do this?
Thanks in advance!
Looks to me like your text inputs don't have an id attribute.
So, you can either give them an id attribute or reference them by the form
Try this where you set your HTML and let me know how it goes:
var html = '<div>'+
'<form name="add_data" action="phpinsert.php">'+
'<input type="text" name="add_lat" id="add_lat" value=""/>'+
'<input type="text" name="add_lng" id="add_lng" value=""/>'+
'</form>'+
'</div>';
OK, figured it out. Google Maps doesn't just put the HTML on the document. I'm not sure what happens behind the scenes, but I think it clones the content somehow. There seems to be 2 input tags with the id of add_lat. So, you have to get the element by it's class name and update it that way. It's the second element in the document. If you use jQuery, you can easily update all elements that have a certain class name anywhere on the page.
Also, I found all kinds of weird things happen with the code you wrote. So, I quickly rewrote it to only have a single marker, and a single infowindow on the map. You can click on the marker to show the current position and drag it to update the position.
Try this code and let me know what you think.
function updateMarkerPosition(latLng)
{
document.getElementsByClassName('add_lat')[1].value = latLng.lat();
document.getElementsByClassName('add_lng')[1].value = latLng.lng();
}
function add_editable_mkr()
{
var popup_form = new google.maps.InfoWindow({
maxWidth:800
});
// Infowindow form
var html = '<div>'+
'<form name="add_data" action="phpinsert.php">'+
'<input type="text" class="add_lat" name="add_lat" value=""/>'+
'<input type="text" class="add_lng" name="add_lng" value=""/>'+
'</form>'+
'</div>';
popup_form.setContent(html);
var marker_drag = new google.maps.Marker(
{
position: map.getCenter(),
draggable:true,
map: map,
});
google.maps.event.addListener(marker_drag, "click", function()
{
popup_form.open(map, marker_drag);
updateMarkerPosition(marker_drag.getPosition());
});
google.maps.event.addListener(marker_drag, 'dragstart', function()
{
popup_form.close();
});
google.maps.event.addListener(marker_drag, 'dragend', function()
{
popup_form.open(map, marker_drag);
updateMarkerPosition(marker_drag.getPosition());
});
google.maps.event.addListener(map, 'click', function(event)
{
marker_drag.setPosition(event.latLng);
});
}
function initialize()
{
map = new google.maps.Map(document.getElementById("map"),
{
center: new google.maps.LatLng(1.364236,103.796082),
zoom: 4,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: true,
mapTypeControlOptions:
{
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
},
navigationControl: true,
navigationControlOptions:
{
style: google.maps.NavigationControlStyle.LARGE
}
});
add_editable_mkr();
}
Goog day!
I have a very simple implementation of google maps, which allows my users to draw polylines anywhere on the map canvas.
When starting afresh, the polyline segments are visible on the map and the polyline is stored inside a mySql database using PHP.
// When editing a map with a polyline:
If a user decides to change the polyline, i load the previous polyline from the database and it is visible to the user. When they click on the button to draw a polyline it wil clear the previous polyline from the map and allow them to start afresh, however, once they start clicking away on the map, the new polyline segments is no visible on the map, but is saves correctly to the database.
Here is what i've done so far:
var map;
var polyline = null;
var polylinePath = null;
// HERE I INITIALIZE MY MAP ETC ETC ...
// IN EDIT MODE I ALLOW THE FOLLOWING FUNCTION TO BE CALLED FIRST TO LOAD ANY EXISITNG POLYLINES FOR THE USER
// THIS WORKS FINE, IT SHOWS THE PREVIOUS POLYLINE ON THE MAP.
function loadBasePolylines($mapGuid)
{
var loadUrl = 'classes/Ajax_System.php';
$.getJSON(loadUrl+"?action=loadMapPolylines&mapId="+mapGuid, function(json) {
if(json[0]['hasPolylines'] == 'yes'){
var polylinesArray = [];
var prepath = polylinePath;
if(prepath){
prepath.setMap(null);
}
var points = json[0].Points;
for (i=0; i<points.length; i++) {
polylinesArray.push(new google.maps.LatLng(points[i].lat,points[i].lon));
bounds.extend(new google.maps.LatLng(points[i].lat,points[i].lon));
}
polyline = new google.maps.Polyline({
strokeColor: json[0]['lineColour'],
strokeOpacity: 0.5,
strokeWeight: json[0]['lineThickness'],
clickable: false,
path: polylinesArray
});
polylinePath = polyline.getPath();
polyline.setMap(map);
map.fitBounds(bounds);
}
});
}
// WHEN THE USER PRESS THE "DRAW PATH" BUTTON, THE FOLLOWING FUNCTION IN QUESTION IS CALLED:
function startPolyline()
{
if(polyline != null){
var answer = confirm("This will clear the current polyline from the map. Are you sure you want to continue?");
if(answer){
polyline.setMap(null);
if (tempMarkers) {
for (i in tempMarkers) {
tempMarkers[i].setMap(null);
}
tempMarkers.length = 0;
}
} else {
return;
}
}
var polyOptions = {
strokeColor: polylineColor,
strokeOpacity: polyLineOpacity,
strokeWeight: polyLineWidth,
clickable: false
}
polyline = new google.maps.Polyline(polyOptions);
polyline.setMap(map);
// Add a listener for the click event
google.maps.event.addListener(map, 'click', addPolylinePoint);
}
function addPolylinePoint(event)
{
var path = polyline.getPath();
path.push(event.latLng);
var marker = new google.maps.Marker({
position: event.latLng,
title: '#' + path.getLength(),
icon: '<?php echo URL; ?>public/mapIcons/mm_20_red.png',
map: map
});
tempMarkers.push(marker);
}
What i've tried so far:
Check the color codes are in correct format: YES
Check Lat Lon order and values: YES
Any help will sincerely be appreciated. Thank you in advance.
Wow, I've solved the disappearing polyline issue after reading through my question again. Hopefully this will help someone out there one day:
In my code where i load the polylines from the database:
function loadBasePolylines($mapGuid)
{
.
.
.
polyline = new google.maps.Polyline({
strokeColor: json[0]['lineColour'],
strokeOpacity: 0.5,
strokeWeight: json[0]['lineThickness'],
clickable: false,
path: polylinesArray
});
}
I set the stroke opacity to 0.5 (Equal to 50%) right...
But then, just when i start the polyline tool in ...
function startPolyline()
var polyOptions = {
strokeColor: polylineColor,
strokeOpacity: polyLineOpacity,
strokeWeight: polyLineWidth,
clickable: false
}
... i set the opacity to an unset variable called polyLineOpacity.
Once i added the global variable and applied the loaded value in ...
function loadBasePolylines($mapGuid)
{
.
.
.
strokeOpacity = json[0]['lineOpacity'] * 100;
polyline = new google.maps.Polyline({
strokeColor: json[0]['lineColour'],
strokeOpacity: strokeOpacity,
strokeWeight: json[0]['lineThickness'],
clickable: false,
path: polylinesArray
});
}
... the polylines with the correct opacity displayed correctly again!
I call this developers brain-freeze.
Tkx!
try reinitiating the map variable in the startPolyline function
map = new google.maps.Map(document.getElementById('id of your map')):
I have this script at the moment, which changes an image when a thumbnail has been changed. I then want JQZOOM to be added to that new image. However, if I put it inside the Onclick event, it gets slower and slower the more times you click on it... I guess because its running multiple instances.
Is there anyway to unbind the JQZOOM from something then rebind it to something else?
Here is my jquery at the moment:
var options = {
zoomWidth: 400,
zoomHeight: 325,
xOffset: 25,
yOffset: 0,
position: "right",
lens: true,
zoomType: "reverse",
imageOpacity: 0.5,
showEffect: "fadein",
hideEffect: "fadeout",
fadeinSpeed: "medium",
title: false
};
$('.jqzoom').jqzoom(options);
$('.single-zoom-image').click ( function () {
$('#bigProductImage').attr("src", $(this).attr("zoom"));
$('.jqzoom').attr("href", $(this).attr("extrazoom"));
});
Thanks in advance if anyone can help me.
Cheers!
This can be done as follows:
Modify jqzoom1.0.1.js where the other mouse functions are (around line 90)
//Handle clicked thumbnails changing the zoomed image
$(this).bind('changeimage', function(){
smallimage = new Smallimage( $("img", this) );
largeimage = new Largeimage(a[0].href);
smallimage.loadimage();
});
Call the zoomer as follows:
$(document).ready(function(){
var options = {
zoomWidth: 300,
zoomHeight: 200,
xOffset: 30,
yOffset: 0,
position: 'right',
title: false,
showPreload: false
};
//Handle clicking on the thumbnails, swap the mainimage and recycle the zoomer
$('.seemore').bind('click', function(e) {
e.preventDefault();
$('.jssProductFullImage').attr('src', $(this).attr('href'));
$('.zoomer').attr('href', $(this).attr('href') );
//Recall the zoomer to update the page
$('.zoomer').trigger('changeimage');
});
$('.zoomer').jqzoom(options);
});
$('.jqzoom').removeData('jqzoom');
Did the job for me.