Posted in ColdFusion | Posted on 03-03-2010 | 4,546 views
A while ago I shared a few emails with a reader who wanted to know if it was possible to do KML operations with CFMAP. For those who don't know, KML is an XML format for geographical data. From what I've read, it allows for various types of overlays and data "skins" over map or Earth data. The reader, Richard Zawadzki, and I went back and forth a bit, but it was he who finally got it working and graciously allowed me to share his code.
To use KML with Google Maps, you simply need to follow the previous examples I've shared - get the map object. ColdFusion gives you the hooks to do so. Once you have it, you can then follow Google's KML docs to work with your data. Here is a quick example of the JavaScript here used. Only the first call is ColdFusion specific:
2
3 var map = ColdFusion.Map.getMapObject("mainMap");
4 map.setCenter(new google.maps.LatLng(28.291889, -81.407793), 9);
5 map.setZoom(9);
6
7 var DRIKML = new GGeoXml("http://maps.google.com/maps/ms?ie=UTF8&hl=en&vps=4&jsv=201b&msa=0&output=nl&msid=101779661887239513456.000465f6105cf06ca4c63");
8 map.addOverlay(DRIKML);
9
10 var ParksKML = new GGeoXml("http://maps.google.com/maps/ms?ie=UTF8&hl=en&vps=5&jsv=201b&oe=UTF8&msa=0&output=nl&msid=101779661887239513456.000465f607e578d46cf83");
11 map.addOverlay(ParksKML);
12
13 var CountyKML = new GGeoXml("http://maps.google.com/maps/ms?ie=UTF8&hl=en&vps=5&jsv=201b&oe=UTF8&msa=0&output=nl&msid=101779661887239513456.00047e28dce659094bcb5");
14 map.addOverlay(CountyKML);
15
16}
You can see a demo of what he came up with here. I've included the complete code below. (And once again - I'm really impressed with everything that can be done with Google Maps!) (Note - I forgot to mention this when I first posted, but Richard asked that I also credit Steve Gongage, his coworker, for working on this as well.)
2<html xmlns="http://www.w3.org/1999/xhtml">
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5<title>Google Maps KML Overlays</title>
6
7<script>
8//<![CDATA[
9function AddKMLOverlay() {
10
11 var map = ColdFusion.Map.getMapObject("mainMap");
12 map.setCenter(new google.maps.LatLng(28.291889, -81.407793), 9);
13 map.setZoom(9);
14
15 var DRIKML = new GGeoXml("http://maps.google.com/maps/ms?ie=UTF8&hl=en&vps=4&jsv=201b&msa=0&output=nl&msid=101779661887239513456.000465f6105cf06ca4c63");
16 map.addOverlay(DRIKML);
17
18 var ParksKML = new GGeoXml("http://maps.google.com/maps/ms?ie=UTF8&hl=en&vps=5&jsv=201b&oe=UTF8&msa=0&output=nl&msid=101779661887239513456.000465f607e578d46cf83");
19 map.addOverlay(ParksKML);
20
21 var CountyKML = new GGeoXml("http://maps.google.com/maps/ms?ie=UTF8&hl=en&vps=5&jsv=201b&oe=UTF8&msa=0&output=nl&msid=101779661887239513456.00047e28dce659094bcb5");
22 map.addOverlay(CountyKML);
23
24}
25
26function init() {
27 AddKMLOverlay();
28}
29//]]>
30</script>
31
32
33</head>
34
35<body>
36
37 <table width="100%" height="100%">
38 <tr>
39 <td align="center">
40 <table>
41
42 <tr align="left" valign="top">
43 <td>
44 <cfmap width="680" height="400" centeraddress="34741" zoomlevel="9" name="mainMap">
45 <cfmapitem name="marker01"
46 address="1 Courthouse Sq, Kissimmee, FL 34741 USA"
47 tip="Osceola County Administration Building"/>
48 </cfmap>
49 </td>
50 </tr>
51
52 </table>
53
54 </td>
55 </tr>
56 </table>
57
58</body>
59</html>
60<cfset ajaxOnLoad("init")>


AddType application/vnd.google-earth.kml+xml kml
We use IIS and it's equally simple to add the correct mime-type.
You also need to make sure that your server is capable of back and forth communication with Google Maps. On our webstage server we were unable to map anything until we actually NAT'ed it out with a public domain.
Thanks Ray for your help and for posting this!
I've been trying to get cfmap to do "larger custom icons" and had some success... but the cfmap and my javascript LatLng don't match up because CF throws an error when it sees a CENTERLONGITUDE greater than -90.0. WHAT??
Try this bit...
<cfmap width="900" height="800" centerlatitude="34.0000" centerlongitude="-118.0000" zoomlevel="13" name="map" showcentermarker="no">
</cfmap>
That should be the center of Los Angeles California... it's a small town maybe CF hasn't heard about it yet.
Any ideas for the readers.
Simply add the negative Longitude from 360 and you'll get the positive number you need to make CFMap work.
<cfset lng = -118.0000>
<cfif lng lt 0>
<cfset lng = (360 + lng)>
</cfif>
<cfmap width="900" height="800" centerlatitude="34.0000" centerlongitude="#lng#" zoomlevel="8" name="map" showcentermarker="yes">
</cfmap>
Adding the mime type to my server made it work. Thanks for sharing this!
John - I'm glad that we were able to help out. That's awesome that it works with other overlay types beyond KML. I searched all over for some reference to doing this and never really found anything. Ray pointed me in the right direction of experimenting with using the standard Google Maps API functions. It never even dawned on me that it would be that simple. The only caveat was that I had to declare the setCenter in the javascript. Leaving that out throws a an error in Firebug and prevents it from adding more than one map layer.
<script>
//<![CDATA[
function AddKMLOverlay(){
var map = ColdFusion.Map.getMapObject("myMap");
map.setCenter(new google.maps.LatLng(44.212,-122.256), 13);
map.setZoom(13);
var boundKML = new GGeoXml("http://andrewsforest.oregonstate.edu/data/spatial/...;);
map.addOverlay(boundKML);
}
function initialize() {
AddKMLOverlay();
}
//]]>
</script>
<table>
<cfoutput>
<h1>#dbcode#</h1>
</cfoutput>
<tr><td>
<cfmap width="680" height="400" centerlatitude="44.212" centerlongitude="-122.256" zoomlevel="9" typecontrol="advanced" name="myMap">
</cfmap>
</td></tr>
</table>
<cfset ajaxOnLoad("initialize")>
any ideas?
will see if I can get one working outside and post links.
http://andrewsforest.oregonstate.edu/data/place_in...
Just to clarify, our development site is on version 9 of CF, but our production site (old site) is on version 5. I'll try and post a link when we go live with the new site. Thanks to Aimee Lesieutre for figuring this out for me.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitiona...;
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Google Maps KML Overlays</title>
<cfajaximport params="#{googlemapkey='ABQIAAAAyBqdTNCO82gdyPcdR94xEBQLxw0Tb61Zhdk6a1DqnmRS3wNprhS8v8KU4__Dlq7yiLtBm5L1kZJSpQ'}#">
<script>
//<![CDATA[
function AddKMLOverlay() {
var map = ColdFusion.Map.getMapObject("mainMap");
map.setCenter(new google.maps.LatLng(33.710413, -117.951616), 9);
map.setZoom(9);
var NorthOC = new GGeoXml("http://maps.google.com/maps/ms?ie=UTF&msa=0&am...;);
map.addOverlay(NorthOC);
}
function init() {
AddKMLOverlay();
}
//]]>
</script>
</head>
<body>
<table width="100%" height="100%">
<tr>
<td align="center">
<table>
<tr align="left" valign="top">
<td>
<cfmap width="680" height="400" centeraddress="92708" zoomlevel="9" name="mainMap">
</cfmap>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
<cfset ajaxOnLoad("init")>
[Add Comment] [Subscribe to Comments]