如何将地理编码结果存储在数据库中

时间:2011-11-04 10:02:42

标签: php mysql google-maps geocoding

我目前正在使用以下代码通过Wordpress数据在Google地图(V3)上绘制多个标记。我正在使用地址(地理编码),并希望避免任何人必须在Wordpress中输入纬度/经度坐标。

目前我在每个请求之间有一段时间延迟以避免地理编码限制,这是不理想的,我需要立即绘制标记。

阅读避免地理编码限制的主题人们建议将地理编码结果存储在数据库中,然后从那里读取数据库中的地理编码结果,而不是再次对标记进行实际地理编码(并使用另一个地理编码请求) )。

我该怎么做?如何将每个标记的地理编码结果存储在数据库中,然后从中读取纬度/经度结果?

p.s我尝试使用Google Fusion Tables,但它不适用于动态数据。

这是我目前的代码:

<script type="text/javascript"> 
(function() { 

window.onload = function() { 
 var mc;
// Creating an object literal containing the properties we want to pass to the map 
var options = { 
zoom: 0, maxZoom: 0, 
center: new google.maps.LatLng(52.40, -3.61), 
mapTypeId: google.maps.MapTypeId.ROADMAP 
}; 

// Creating the map 
var map = new google.maps.Map(document.getElementById('map'), options); 

// Creating a LatLngBounds object 
var bounds = new google.maps.LatLngBounds(); 

// Creating an array that will contain the addresses 
var places = []; 

// Creating a variable that will hold the InfoWindow object <img src="'.$fields->company_logo.'" /> 
var infowindow; 
mc = new MarkerClusterer(map);
<?php
$pages = get_pages(array('child_of' => $post->ID, 'sort_column' => 'menu_order'));
$popup_content = array();
foreach($pages as $post)
    {
    setup_postdata($post);
    $fields = get_fields(); 
    $popup_content[] = '<p>'.$fields->company_name.'</p><br /><a href="'.get_page_link($post->ID).'">View profile</a>';
    $comma = ", ";
    $full_address = "{$fields->address_line_1}{$comma}{$fields->address_line_2}{$comma}{$fields->address_line_3}{$comma}{$fields->post_code}";
    $address[] = $full_address;
    }
wp_reset_query();
echo 'var popup_content = ' . json_encode($popup_content) . ';';
echo 'var address = ' . json_encode($address) . ';';
?>

var geocoder = new google.maps.Geocoder(); 

var markers = [];

// Adding a LatLng object for each city 
function geocodeAddress(i) {
     geocoder.geocode( {'address': address[i]}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            places[i] = results[0].geometry.location;

            // Adding the markers 
            var marker = new google.maps.Marker({position: places[i], map: map});
            markers.push(marker);
            mc.addMarker(marker);

            // Creating the event listener. It now has access to the values of i and marker as they were during its creation
            google.maps.event.addListener(marker, 'click', function() {
                // Check to see if we already have an InfoWindow
                if (!infowindow) {
                    infowindow = new google.maps.InfoWindow();
                }

                // Setting the content of the InfoWindow
                infowindow.setContent(popup_content[i]);

                // Tying the InfoWindow to the marker 
                infowindow.open(map, marker);
            });

            // Extending the bounds object with each LatLng 
            bounds.extend(places[i]); 

            // Adjusting the map to new bounding box         alert("Geocode was not successful for the following reason: " + status + popup_content[i]);  
            map.fitBounds(bounds) 
        } else { 

        }
    })
}

function geocode() {
    if (geoIndex < address.length) {
        geocodeAddress(geoIndex);
        ++geoIndex;
    }
    else {
        clearInterval(geoTimer);
    }
}
var geoIndex = 0;
var geoTimer = setInterval(geocode, 1000);  // Delay

var markerCluster = new MarkerClusterer(map, markers); 
} 
})
(); 
</script> 

更新: 经过进一步研究,我发现了这一点 - How do I use Google Maps geocoder.getLatLng() and store its result in a database?Can't Deserialize GoogleMaps DirectionsResult Object。它们似乎是我需要的一般事物,但不知道如何将它作为我的例子。

0 个答案:

没有答案