2014-10-27 66 views
-1

我有下面的代碼,它從PHP中的地址變量創建Google地圖。需要從谷歌地圖獲得當前的經緯度

的Javascript:

function initialiseGoogleMaps(selector) { 
    geocoder = null; 
    maps = []; 
    bounds = []; 

    if (!selector) { 
     selector = '.googleMap'; 
    } 

    $.each($(selector), function(index, value) { 
     var $this = $(value), 
      mapOptions = $this.data(), 
      addresses = $this.data('addresses') + ''; 

     addresses = addresses ? addresses.split(',') : []; 
     $this.attr('id', 'google-map-' + index); 

     maps[index] = new google.maps.Map(this, mapOptions); 
     bounds[index] = new google.maps.LatLngBounds(); 

     for (var i=0; i < addresses.length; i++) { 
      if (addresses[i] === '') { 
       continue; 
      } 

      initialiseGoogleGeocoder().geocode({'address': addresses[i]}, function(results, status) { 
       if (status == google.maps.GeocoderStatus.OK) { 
        placeMarker(maps[index], bounds[index], results[0].geometry.location, addresses[i]); 
       } 
      }); 
     } 
    }); 
} 

function placeMarker(map, bounds, location, text) { 
    var marker = new google.maps.Marker({ 
     map: map, 
     position: location, 
     title: text 
    }); 

    bounds.extend(marker.position); 
    map.fitBounds(bounds); 
    var zoom = map.getZoom(); 
    map.setZoom(zoom > 6 ? 6 : zoom); 
} 

function initialiseGoogleGeocoder() { 
    if (geocoder === null) { 
     geocoder = new google.maps.Geocoder(); 
    } 
    return geocoder; 
} 

PHP/HTML和Javascript火:

$distinctPostcodes = array_filter(array_unique(Hash::extract($addresses, '{n}.Address.postcode'))); 

     if (!empty($distinctPostcodes)) { 
     echo $this->Html->tag('div', '', array(
      'class' => 'googleMap', 
      'id' => 'google-map-' . $uuid, 
      'data-addresses' => implode(',', $distinctPostcodes), 
      'data-zoom' => 5, 
     )), 
     $this->Html->link('Fix Map', array(
      'controller' => 'addresses', 
      'action' => 'fixmap', $address['company_id'], 
     )); 
    } 

<script type="text/javascript"> 
    $(document).ready(function() { 
     initialiseGoogleMaps($('#google-map-<?php echo $uuid; ?>')); 
    }); 
</script> 

我需要做的,是當用戶移動地圖到不同的位置是什麼,我需要搶來自DOM的經緯度並通過POST請求將其保存到mySQL中。

我都對着DOM的谷歌地圖,它顯示LAT和LONG,但在地圖上移動他們不改變。

任何人都可以幫忙嗎?

回答

1

你要聽如bounds_changedcenter_changeddragend(取決於你到底想要什麼)的地圖事件,獲取地圖中心座標(如果這就是你想要的),並把它傳遞到後端,最有可能借助AJAX請求。

這裏可用圖中的事件:https://developers.google.com/maps/documentation/javascript/reference#Map

例如:

google.maps.event.addListener(map, 'center_changed', function() { 

    var center = map.getCenter(); 

    // AJAX request to your backend with the 'center' variable 
}); 
+0

我試圖添加代碼如下:\t $(文件)。就緒(函數(){ \t \t initialiseGoogleMaps( $( '#谷歌地圖 - <?PHP的回聲$ UUID;?>')); \t \t \t變種地圖= '#谷歌地圖 - '; \t \t \t google.maps.event.addListener(地圖, 'center_changed的',函數(){ \t \t VAR中心= map.getCenter(); \t \t \t \t \t的console.log(中心); \t \t \t}); \t});但我得到的錯誤:TypeError:c是未定義的? – 2014-10-28 12:09:41

+1

那是什麼? 'var map ='#google-map - <?php echo $ uuid; ?>請提供您的代碼**輸出**(由您的PHP打印的內容)。該行通常應爲:'VAR地圖=新google.maps.map(元件,的MapOptions);' – MrUpsidown 2014-10-28 12:13:42

+0

2014-10-28 13:34:17

相關問題