我正在尝试制作一个小型应用程序,该应用程序需要一个城市和州,并将地址分配到经纬度的地理编码。现在,我正在使用Google Map的API,ColdFusion和SQL Server。基本上,“城市”和“州”字段位于数据库表中,我想获取这些位置并将标记放置在Google map 上以显示它们的位置。

这是我进行地理编码的代码,查看页面的源代码表明它正确循环了我的查询,并在地址字段中放置了位置(“Omaha,NE”),但是没有标记或 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> }

这是初始化 map 的代码:
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);
}

我确实有一个使用纬度/经度的 map 工作,该经纬度已被硬编码到数据库表中,但是我希望能够仅使用城市/州并将其转换为纬度/经度。有什么建议或方向吗?将纬度/经度存储在数据库中也是可能的,但是我不知道如何在SQL中执行此操作。

最佳答案

您可能需要考虑以下示例:

使用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,而不是在示例中使用硬编码的数组。

上面示例的屏幕截图:

07-24 09:49
查看更多