Loading city/state from SQL Server to Google Maps?

蓝咒 提交于 2019-12-04 05:25:49
Daniel Vassallo

You may want to consider the following example:

Using the 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>

Using the 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>

All you need to do is to render the JavaScript array locations from ColdFusion, instead of using the hardcoded one in the example.

Screenshot from the above example:

You need to actually add the marker to the map using the addOverlay method:

var point = new GLatLng(...);
map.addOverlay(new GMarker(point));

You can also add instances of the Marker class to your map:

map.addOverlay(marker);

See the Map Overlays docs:

http://code.google.com/apis/maps/documentation/javascript/v2/overlays.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!