我試圖做一個小型應用程序,需要一個城市&狀態和地址解析到經緯度位置。現在我正在使用Google Map的API,ColdFusion和SQL Server。基本上,城市和州的字段都放在數據庫表中,我想要將這些位置放在Google地圖上,並將其放在Google地圖上,以顯示它們的位置。將城市/州從SQL Server加載到Google地圖?
這是我的代碼來做地理編碼,並查看頁面的源代碼顯示它正確循環我的查詢,並在地址字段中放置一個位置(「奧馬哈,NE」),但沒有標記,或地圖對於這個問題,是顯示在頁面上:
function codeAddress() {
<cfloop query="GetLocations">
var address = document.getElementById(<cfoutput>#Trim(hometown)#,#Trim(state)#</cfoutput>).value;
if (geocoder) {
geocoder.geocode({<cfoutput>#Trim(hometown)#,#Trim(state)#</cfoutput>: address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
title: <cfoutput>#Trim(hometown)#,#Trim(state)#</cfoutput>
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
</cfloop> }
,這裏是代碼初始化地圖:
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(42.4167,-90.4290);
var myOptions = {
zoom: 5,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: "Test"
});
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}
我有一個使用緯度/長,這是很難的地圖工作編碼到數據庫表中,但我希望能夠使用城市/州並將其轉換拉長/長。任何建議或指示?在數據庫中存儲經緯度也是可能的,但我不知道如何在SQL中做到這一點。
非常感謝丹尼爾,我照你說的和將我查詢到的位置變化並輸出一個數組裏面。像魅力一樣工作! – knawlejj 2010-05-25 03:07:42
@ knawlejj:這是個好消息。我很高興它的工作:) – 2010-05-25 03:09:42