How to update the latlng from javascript to php in google maps v3
I have to show a bunch of markers on the map after the map has been dragged. How do i pass the current map center from javascript to my php code to get the data? The latlng is not getting updated.
var CHANGE_MARKERS = false;
var CITY_MAP_CENTER_LAT= '12.971598';
var CITY_MAP_CENTER_LNG= '77.594569';
var CITY_MAP_ZOOMING_FACT= 15;
var CITY_MAP_VIEW_TYPE = '';
var LAT = "";
var LNG = "";
var LATLNG = "";
var A1,A2,A3,A4;
var map;
var mgr;
var mc = null;
var markerClusterer = null;
var showMarketManager = false;
var infoWindow;
var markersarray = [];
var allmarkers = [];
function setLatLng(){
if(CHANGE_MARKERS == false){
LAT = '12.971598';
LNG = '77.594569';
}
else{
var ctr = map.getCenter();
LAT =ctr.lat();
LNG =ctr.lng();
}
}
function setMarkers(){
allmarkers.length = 0;
allmarkers = [];
var markers = [];
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert(<?php echo $_SESSION['center']; ?>);
//var markersData = xmlhttp.responseText;
//alert(markersData);
markers = {'data':[
<?php
?>
]};
for (var level in markers) {
//alert(markers[level].length);
}
}
}
xmlhttp.open("GET","../subscriber_listing/google.php?center=" + map.getCenter(),false);
xmlhttp.send();
if (markers) {
for (var level in markers) {
//alert(markers[level].length);
for (var i = 0; i < markers[level].length; i++) {
var details = markers[level][i];
//alert(details.name);
var image = new google.maps.MarkerImage(details.icons,new google.maps.Size(PIN_POINT_ICON_WIDTH, PIN_POINT_ICON_H开发者_运维百科EIGHT));
var myLatLng = new google.maps.LatLng(details.location[0], details.location[1]);
var newmarker = createMarker(details.name,myLatLng,image);
allmarkers.push(newmarker);
}
mgr.addMarkers( allmarkers, 0 );
}
mgr.refresh();
}
}
function doSearch(){
display();
CHANGE_MARKERS = true;
initialize();
}
function createMarker(name,latlng,image){
var marker = new google.maps.Marker({
title: name,
position: latlng,
icon: image,
clickable: true,
draggable: false,
flat: true
});
return marker;
}
function display(){
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
}
}
xmlhttp.open("GET","../subscriber_listing/google.php?center=" + map.getCenter(),false);
xmlhttp.send();
}
if(CITY_MAP_CENTER_LAT=='')
{
var CITY_MAP_CENTER_LAT = 34;
}
if(CITY_MAP_CENTER_LNG=='')
{
var CITY_MAP_CENTER_LNG = 0;
}
if(CITY_MAP_CENTER_LAT!='' && CITY_MAP_CENTER_LNG!='' && CITY_MAP_ZOOMING_FACT!='')
{
var CITY_MAP_ZOOMING_FACT = 13;
}else if(CITY_MAP_ZOOMING_FACT!='')
{
var CITY_MAP_ZOOMING_FACT = 3;
}
var PIN_POINT_ICON_HEIGHT = 32;
var PIN_POINT_ICON_WIDTH = 20;
if(CITY_MAP_VIEW_TYPE=='TERRAIN')
{
var maptype= google.maps.MapTypeId.TERRAIN;
}else
if(CITY_MAP_VIEW_TYPE=='SATELLITE')
{
var maptype= google.maps.MapTypeId.SATELLITE;
}else
if(CITY_MAP_VIEW_TYPE=='')
{
var maptype= google.maps.MapTypeId.ROADMAP;
}
if(MAP_DISABLE_SCROLL_WHEEL_FLAG)
{
var MAP_DISABLE_SCROLL_WHEEL_FLAG = 'No';
}
function initialize() {
setLatLng();
var myOptions = {
zoom: CITY_MAP_ZOOMING_FACT,
center: new google.maps.LatLng(LAT, LNG),
mapTypeId: maptype
}
map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
mgr = new MarkerManager(map);
infoWindow = infoWindow = new google.maps.InfoWindow();
google.maps.event.addListener(mgr, 'loaded', function(){
setMarkers();
});
google.maps.event.addListener(map, "dragend", function() {
doSearch();
});
}
google.maps.event.addDomListener(window, 'load', initialize);
Getting the lat/lng values in JavaScript API v3:
map.getCenter().lat() // latitude in degrees
map.getCenter().lng() // longitude in degrees
To retrieve those values after the map has been dragged, add a listener for the center_changed
and/or dragend
events.
Since all this happens after the map has been rendered--that is, after any server-side PHP has already run--you'll need to use AJAX or some other technology to send the values to the server if you need to process them with PHP for whatever reason.
精彩评论