直截了当:我正在构建一个带有通过 JSON 提取的自定义标记的 gmap。我还需要提供搜索功能;最接近输入邮政编码 (AU) 的标记。
标记显示(和数组中的其他数据)工作正常,这是第二部分让我大吃一惊。我不擅长 JS。目前我要做的就是返回输入邮政编码的纬度和经度,但如果有人能告诉我如何执行整个功能,那就太好了。谁能指出我正确的方向?JS 和下面的标记,并提前感谢您的帮助。
JavaScript:
function initialize() {
/* Map setup */
var latlng = new google.maps.LatLng(XXX, XXX);
var mapOptions = {
center: latlng,
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
/* Map initialise */
var map = new google.maps.Map(document.getElementById("map-canvas"),
mapOptions);
/* set iconpath */
var iconBase = '/path-to-icons';
/* create a separate shadow icon */
var markerShadow = {
url: iconBase + 'icon_map-shadow.png',
anchor: new google.maps.Point(15, 25)
};
/* Get JSON */
jQuery.getJSON('/path-to/markers.json', function (mapdata) {
$.each(mapdata, function (key, data) {
var latlng = new google.maps.LatLng(data.lat, data.lng);
var marker = new google.maps.Marker({
position: latlng,
map: map,
icon: iconBase + 'icon_map.png',
shadow: markerShadow,
address: data.address,
subaddress: data.subaddress,
title: data.title
});
/* For the showing of the details */
// TODO : MAKE THIS NEATER
google.maps.event.addListener(marker, 'click', function () {
$("#map-details .inner").hide();
$("#map-details .inner span").html("");
$("#map-details .inner .neareststore").html(marker.title);
$("#map-details .inner .address").html(marker.address);
$("#map-details .inner .sub-address").html(marker.subaddress);
$("#map-details .inner").fadeIn(200);
});
});
});
};
google.maps.event.addDomListener(window, 'load', initialize);
function geocode() {
$("#search").submit(function(e){
e.preventDefault();
var currentloc = $("#postcode").val();
var url = "http://maps.googleapis.com/maps/api/geocode/json?address="+currentloc+"+AU&sensor=false";
$.getJSON(url, function(data) {
//JSON function
});
};
};
基本表单 HTML:
<form action="" method="post" id="search">
<fieldset>
<input name="postcode" id="postcode">
<input type="submit" id="postcodesearch" value="search">
</fieldset>
</form>
* 编辑 * - 已回答 还不能回答我自己的问题,但已经使用此处链接的 Haversine 公式解决了这个问题:Google Maps Api v3 - 查找最近的标记
它需要一些整理,但这是要点,供后人参考。感谢大家的帮助。
function rad(x) {return x*Math.PI/180;}
function find_closest_marker( _lat , _lng ) {
var lat = _lat;
var lng = _lng;
var R = 6371; // radius of earth in km
var distances = [];
var closest = -1;
for( i=0;i<map.markers.length; i++ ) {
var mlat = map.markers[i].position.lat();
var mlng = map.markers[i].position.lng();
var dLat = rad(mlat - lat);
var dLong = rad(mlng - lng);
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(rad(lat)) * Math.cos(rad(lat)) * Math.sin(dLong/2) * Math.sin(dLong/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
distances[i] = d;
if ( closest == -1 || d < distances[closest] ) {
closest = i;
}
}
var closest = map.markers[closest];
map.setCenter(closest.getPosition());
$("#map-details .inner").hide();
$("#map-details .inner span").html("");
$("#map-details .inner .neareststore").html(closest.title);
$("#map-details .inner .address").html(closest.address);
$("#map-details .inner .sub-address").html(closest.subaddress);
$("#map-details .inner").fadeIn(200);
}
$(document).ready(function(){
$("#search").submit(function(e){
e.preventDefault();
var currentloc = $("#postcode").val();
var url = "http://maps.googleapis.com/maps/api/geocode/json?address="+currentloc+"+AU&sensor=false";
$.getJSON(url, function(data) {
var lat = data.results[0].geometry.location.lat;
var lng = data.results[0].geometry.location.lng;
find_closest_marker(lat,lng)
});
});
});