从SQL Server加载城市/州到Google Maps?

6
我正在尝试制作一个小应用程序,它接收城市和州信息,并将该地址地理编码为纬度/经度位置。目前我正在利用Google Map的API、ColdFusion和SQL Server。基本上,城市和州字段在数据库表中,我想获取这些位置并在Google Map上放置标记以显示它们的位置。
以下是我的地理编码代码,查看页面源代码显示它正确地循环查询并将位置(“奥马哈,内布拉斯加州”)放入地址字段,但没有标记或地图出现在页面上:
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中实现。

2个回答

4

你可能会想考虑下面的例子:

使用V2 API

<!DOCTYPE html>
<html> 
<head> 
  <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
  <title>Google Maps API Geocoding Demo</title> 
  <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false"
          type="text/javascript"></script> 
</head> 
<body onunload="GUnload()"> 

  <div id="map_canvas" style="width: 400px; height: 300px"></div> 

  <script type="text/javascript"> 

    // Prepare this list from ColdFusion
    var locations = [
       'New York, NY',
       'Los Angeles, CA',
       'Chicago, IL',
       'Houston, TX',
       'Phoenix, AZ'
    ];

    if (GBrowserIsCompatible()) {
       var map = new GMap2(document.getElementById("map_canvas"));

       var geocoder = new GClientGeocoder();
       var index = 0;

       var geocoderFunction = function () { 
          geocoder.getLatLng(locations[index], function (point) {    
             if (point) {
                map.addOverlay(new GMarker(point));                
             }

             // Call the geocoder with a 100ms delay
             index++;
             if (locations.length > index) {
                setTimeout(geocoderFunction, 100);
             }
          });
       }

       map.setCenter(new GLatLng(38.00, -100.00), 3);

       // Launch the geocoding process
       geocoderFunction();
    }
  </script> 
</body>
</html>

使用 V3 API

<!DOCTYPE html>
<html> 
<head> 
  <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
  <title>Google Maps API Geocoding Demo</title> 
  <script src="http://maps.google.com/maps/api/js?sensor=false" 
          type="text/javascript"></script>
</head> 
<body> 

  <div id="map_canvas" style="width: 400px; height: 300px"></div> 

  <script type="text/javascript"> 

    // Prepare this list from ColdFusion
    var locations = [
       'New York, NY',
       'Los Angeles, CA',
       'Chicago, IL',
       'Houston, TX',
       'Phoenix, AZ'
    ];

    var mapOpt = { 
       mapTypeId: google.maps.MapTypeId.TERRAIN,
       center: new google.maps.LatLng(38.00, -100.00),
       zoom: 3
    };

    var map = new google.maps.Map(document.getElementById("map_canvas"), mapOpt);

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

    var geocoderFunction = function () { 
       geocoder.geocode({ 'address': locations[index] }, function (results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
             new google.maps.Marker({
                map: map, 
                position: results[0].geometry.location
             });             
          }

          // Call the geocoder with a 100ms delay
          index++;
          if (locations.length > index) {
             setTimeout(geocoderFunction, 100);
          }
       });
    }

    // Launch the geocoding process
    geocoderFunction();
  </script> 
</body>
</html>

你只需要从ColdFusion中渲染JavaScript数组locations,而不是使用示例中的硬编码数组。

上面示例的截图:

Google Maps API Geocoding Demo


非常感谢你,Daniel。我按照你说的方法将我的查询转换为了一个数组,并将其输出到了locations变量中。效果非常好! - knawlejj

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接