谷歌地图api v2 |商店查找器/定位器

时间:2011-11-17 15:43:35

标签: jquery google-maps

我有一个名为“商店定位器”的网页,它使用谷歌地图api v2。当你在这个页面上时它一切都很好但我想添加一个侧边栏表格输入,允许网站其他页面上的用户能够输入地址,然后转发到“商店定位器”页面,显示他们的地图/结果。

所以我的问题是这样的:我如何修改下面的代码,以便能够从我网站上的任何页面将表格/位置数据发送到地图api,并在我的商店定位页面上获得结果?

任何帮助都非常感激,因为这让我发疯! S.(代码如下;我省略了api键部分,正文onload东西和xml / php处理)

地图代码

var map;
var geocoder;

var iconN = new GIcon(); 
iconN.image = 'images/marker.png';
iconN.iconSize = new GSize(33, 12);
iconN.iconAnchor = new GPoint(28, 43);
iconN.infoWindowAnchor = new GPoint(65, 12);

function load() {
  if (GBrowserIsCompatible()) {
    geocoder = new GClientGeocoder();
    map = new GMap2(document.getElementById('map'));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(53.2415, -1.6809), 4);
  }
}
function searchLocations() {     
    var address = document.getElementById('addressInput').value;
    geocoder.getLatLng(address, function(latlng) {
    if (!latlng) {
        alert(address + ' not found');
    } else {
    searchLocationsNear(latlng);         
    }
    }); 
}
function searchLocationsNear(center) {
    var radius = document.getElementById('radiusSelect').value;
    var searchUrl = 'xml/phpsqlsearch_genxml.php?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius;
    GDownloadUrl(searchUrl, function(data) {
        var xml = GXml.parse(data);
        var markers = xml.documentElement.getElementsByTagName('marker');
        map.clearOverlays();
        var results = document.getElementById('results');
        results.innerHTML = '';
        if (markers.length == 0) {
            results.innerHTML = '<div>No results found.</div>';
            map.setCenter(new GLatLng(53.2415, -1.6809), 4);
        return;
    }

    var bounds = new GLatLngBounds();
    for (var i = 0; i < markers.length; i++) {
        var name = markers[i].getAttribute('name');
        var address = markers[i].getAttribute('address');
        var distance = parseFloat(markers[i].getAttribute('distance'));
        var point = new GLatLng(parseFloat(markers[i].getAttribute('lat')),
                                parseFloat(markers[i].getAttribute('lng')));
        var phone = markers[i].getAttribute('phone');
        var website = markers[i].getAttribute('website');
        var logo = markers[i].getAttribute('logo');

        var marker = createMarker(point, name, address, phone, website, logo);
        map.addOverlay(marker);
        var sidebarEntry = createSidebarEntry(marker, name, address, distance, phone, website, logo);
        results.appendChild(sidebarEntry);
        bounds.extend(point);
    }
    map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
    });
}

function createMarker(point, name, address, phone) {
  var marker = new GMarker(point, iconN);
  var html = '<strong>' + name + '</strong> <br/>' + address + '<br />Tel: ' + phone;
  GEvent.addListener(marker, 'click', function() {
    marker.openInfoWindowHtml(html);
  });
  return marker;
}

function createSidebarEntry(marker, name, address, distance, phone, website, logo) {
    var div = document.createElement('div');
    var html = '<img src="images/stockist-logos/' + logo + '" height="102" alt="' + name + '" class="right" /> <h3>' + name + '</h3> <small>Distance: <strong>' + distance.toFixed(1) + ' miles</strong></small><br/><p>' + address + '</p><p>Tel: ' + phone + '</p>';    
    div.innerHTML = html;      
    GEvent.addDomListener(div, 'click', function() {
        GEvent.trigger(marker, 'click');
    });
    GEvent.addDomListener(div, 'mouseover', function() {
        div.style.backgroundColor = '#fff';
    });
    GEvent.addDomListener(div, 'mouseout', function() {
        div.style.backgroundColor = '#f7f7f7';
    });
    return div;   
}

表单/ html代码

<form action="#" id="stockists" onsubmit="searchLocations()">
<label>Location: </label><input type="text" id="addressInput" />
<label>Distance (miles): </label>
<select id="radiusSelect">
<option value="25" selected>25 </option>
<option value="100">100 </option>                        
<option value="200">200 </option>
</select>
<input type="submit" value="Search" />     
</form>

1 个答案:

答案 0 :(得分:1)

将表单放在主要内容的旁边,使表单发布/到达您的处理页面

<form action="posting_page.php" method="get" id="stockists">
<label>Location: </label><input type="text" id="addressInput" />
<label>Distance (miles): </label>
<select id="radiusSelect">
<option value="25" selected>25 </option>
<option value="100">100 </option>                        
<option value="200">200 </option>
</select>
<input type="submit" value="Search" />
</form>

然后使用php或javascript获取查询字符串/表单值

function searchLocations() {     
    var address = <GETFROMPHP>;
    geocoder.getLatLng(address, function(latlng) {
    if (!latlng) {
        alert(address + ' not found');
    } else {
    searchLocationsNear(<GETFROMPHP>);         
    }
    }); 
}

php不是我的主要开发语言,因此您需要查找如何获取查询字符串/表单值。您还可以将处理页面上的输入字段设置为post / get中的值,这样您就不必更改脚本。如果您需要更多帮助,请告诉我

相关问题