Accueil/modules/gmap/js/gmap.js

90 lines
2.9 KiB
JavaScript
Raw Normal View History

2010-09-27 12:48:53 +02:00
$(document).ready(function(){
var geocoder = new google.maps.Geocoder();
var map;
var gmarkers = [];
var infowindow;
var directions = new google.maps.DirectionsRenderer();
var directionsService = new google.maps.DirectionsService();
var current_lat = 0;
var current_lng = 0;
function getCurrentLat() {
return current_lat;
}
function getCurrentLng() {
return current_lng;
}
function addMarker(latlng,title,content,category,icon) {
var marker = new google.maps.Marker({
map: map,
title : title,
icon: new google.maps.MarkerImage(icon, new google.maps.Size(57,34)),
position: latlng
});
var html = '<div style="float:left;text-align:left;width:250;">'+content+'</div>'
google.maps.event.addListener(marker, "click", function() {
if (infowindow) infowindow.close();
infowindow = new google.maps.InfoWindow({content: html});
infowindow.open(map,marker);
map.setCenter(new google.maps.LatLng(latlng.lat(),latlng.lng()),3);
});
marker.mycategory = category;
gmarkers.push(marker);
}
function geocodeMarker(address,title,content,category,icon) {
if (geocoder) {
geocoder.geocode( { "address" : address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latlng = results[0].geometry.location;
addMarker(results[0].geometry.location,title,content,category,icon)
}
});
}
}
function geocodeCenter(address) {
if (geocoder) {
geocoder.geocode( { "address": address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
}
function initialize() {
var myLatlng = new google.maps.LatLng(48.8792,2.34778);
var myOptions = {
zoom: 11,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("mymap"), myOptions);
geocodeCenter("Paris France");
google.maps.event.addListener(map,"click",function(event) { if (event) { current_lat=event.latLng.lat();current_lng=event.latLng.lng(); }}) ;
directions.setMap(map);
directions.setPanel(document.getElementById("route"))
}
initialize();
$('#menu-bar').append($('#myAdress'));
$('#map').css({'visibility':'hidden'});
$('#gmapLink').fancybox({
'transitionIn': 'elastic',
'transitionOut': 'elastic',
'onClosed': function(){$('#map').css({'visibility':'hidden'});},
'onStart': function(){$('#map').css({'visibility':'visible'});}
});
$('#myAdressField').keydown(function(e){
var code = (e.keyCode ? e.keyCode : e.which);
if(code == 13) {
e.preventDefault();
var adress = $(this).val();
geocodeMarker(adress,adress,adress,'','http://maps.gstatic.com/intl/fr_ALL/mapfiles/markers/marker_sprite.png');
geocodeCenter(adress);
$('#gmapLink').trigger('click');
}
});
});