我是这里的新手,想就这个问题寻求帮助。我有一个代码,应该映射并列出离用户当前位置最近的 5 个诊所。这个诊所取自我上传的融合表数据。这是我拥有的代码:
function initialize() {
var map = new google.maps.Map(document.getElementById('map-canvas'), {
center: new google.maps.LatLng(3.148, 101.715),
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var layer = new google.maps.FusionTablesLayer({
query: {
select: 'Location',
from: '16JfuLxarMf5UjxThG2Tno_SZoNSZ_ExwbQyCeu0',
orderBy: 'ST_DISTANCE(Location, LATLNG(3.148, 101.715))',
limit: 6
}
});
layer.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
我上面的代码可以绘制最近的诊所,但它取决于坐标。我想要的是,它可以自动检测用户当前位置并根据该位置映射和列出诊所。
我有如下的地理位置代码:
window.onload = function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(handle_geo);
$("#queryInput").change(doSearch);
} else {
var map = document.getElementById("map-canvas");
map.innerHTML = "Geolocation is not available... have you considered a forward-thinking browser?";
}
};
但是,当我将代码组合成这样时:
window.onload = function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(handle_geo);
$("#queryInput").change(doSearch);
} else {
var map = document.getElementById("map-canvas");
map.innerHTML = "Geolocation is not available... have you considered a forward-thinking browser?";
}
};
function handle_geo(position){
// Initialize the map with default UI.
gMap = new google.maps.Map(document.getElementById("map-canvas"), {
center: new google.maps.LatLng(position.coords.latitude, position.coords.longitude),
zoom: 15,
mapTypeId: 'roadmap'
});
var layer = new google.maps.FusionTablesLayer({
query: {
select: 'Location',
from: '16JfuLxarMf5UjxThG2Tno_SZoNSZ_ExwbQyCeu0',
orderBy: 'ST_DISTANCE(Location,'+map.getCenter().lat()+','+map.getCenter().lng()+'))',
limit: 4
}
});
layer.setMap(map);
}
google.maps.event.addDomListener(window, 'load', handle_geo);
它仅设法放大用户当前位置,但没有映射和列出最近的诊所。那么我该如何让它发挥作用呢?提前致谢。