A reader asked:
cfmap is very new to me. Could it allow me to get click a map and set variables for the longitude and latitude of the point where I clicked? If so, how? That would be great for recording bird sightings!
I had not really looked much at Google Maps before ColdFusion 9 introduced it to me, but if there is one thing I've learned - Google Maps can do pretty much about anything. This particular request is actually really darn easy. Here is one way to solve the problem.
First, let's create a simple map and listen for the page to be complete. We will then grab the Map object using ColdFusion's JavaScript API. This will give us a hook to the proper Google Map object.
You can play with a demo here. Right now it isn't very exciting. We should probably add markers, and make the form a bit more complex. For example, most users probably don't need to see the actual latitude/longitude, but they probably want to enter data ("The bird was awesome, man. It had wings. And it flew. Cool."). In my next entry I'll add these features to the form.
1 <html>
2
3 <head>
4 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
5
6 <script>
7
8 function init() {
9 map = ColdFusion.Map.getMapObject('mainMap')
10 }
11 </script>
12 </head>
13
14 <body>
15 <h2>Bird Spotting Form</h2>
16
17 <cfmap name="mainMap" centeraddress="Lafayette, LA" showcentermarker="false" zoomlevel="13">
18 <p>
19 Click on the map to record where you saw a bird.
20 </p>
21
22 <form id="mainForm">
23 <input type="submit" value="Send Report" id="submitButton" style="display:none">
24 </form>
25
26 </body>
27 </html>
28 <cfset ajaxOnLoad("init")>
I've got a simple map centered on my home town. I put a form at the bottom which is empty for now. The last line, ajaxOnLoad, simple tells ColdFusion what JavaScript I need to run when the page is done loaded. Right now my init() function simply grabs the Google Map object.
I then did a quick Google (is it weird to use Google to search for Google docs?) on map events and came across a pretty nice set of documentation. As powerful as Google's APIs are, I'm not always quite as pleased with their docs or ease of use. In this case, it looks like all we need to do is add a simple event listener. This event listener will be called with either an overlay object or a longitude/latitude pair:
2
3 <head>
4 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
5
6 <script>
7
8 function init() {
9 map = ColdFusion.Map.getMapObject('mainMap')
10 }
11 </script>
12 </head>
13
14 <body>
15 <h2>Bird Spotting Form</h2>
16
17 <cfmap name="mainMap" centeraddress="Lafayette, LA" showcentermarker="false" zoomlevel="13">
18 <p>
19 Click on the map to record where you saw a bird.
20 </p>
21
22 <form id="mainForm">
23 <input type="submit" value="Send Report" id="submitButton" style="display:none">
24 </form>
25
26 </body>
27 </html>
28 <cfset ajaxOnLoad("init")>
1 GEvent.addListener(map, "click", function(overlay,latlng) {
2 })
For my demo I decided that each click would simply take the longitude and latitude and add them as text fields to a form. I whipped up the following JavaScript:
2 })
1 <script>
2 var counter;
3
4 function init() {
5 counter = 0
6 map = ColdFusion.Map.getMapObject('mainMap')
7 GEvent.addListener(map, "click", function(overlay,latlng) {
8 if(latlng) {
9 counter++;
10 var s = '<b>Report '+counter+'</b>: '
11 s += '<input type="text" name="siteport_' + counter + '" value="'+latlng+'" size="50"><br/>'
12 $("#submitButton").before(s)
13 $("#submitButton").show()
14 }
15 })
16 }
17 </script>
For the most part this should be pretty trivial. I keep a counter so I can have unique numbers for each sighting. I used a text field with a dynamic name and value... and that's it. Here is a quick screen shot:
2 var counter;
3
4 function init() {
5 counter = 0
6 map = ColdFusion.Map.getMapObject('mainMap')
7 GEvent.addListener(map, "click", function(overlay,latlng) {
8 if(latlng) {
9 counter++;
10 var s = '<b>Report '+counter+'</b>: '
11 s += '<input type="text" name="siteport_' + counter + '" value="'+latlng+'" size="50"><br/>'
12 $("#submitButton").before(s)
13 $("#submitButton").show()
14 }
15 })
16 }
17 </script>
You can play with a demo here. Right now it isn't very exciting. We should probably add markers, and make the form a bit more complex. For example, most users probably don't need to see the actual latitude/longitude, but they probably want to enter data ("The bird was awesome, man. It had wings. And it flew. Cool."). In my next entry I'll add these features to the form.


Comment 1 written by Bob on 19 March 2010, at 12:09 PM
Comment 2 written by Raymond Camden on 19 March 2010, at 12:53 PM
Comment 3 written by Ben Nadel on 30 March 2010, at 2:02 PM
Comment 4 written by Jody Fitzpatrick on 10 April 2010, at 10:26 AM
For Example.)
Marker one location is in NY, marker two is in Louisiana, and marker three is in California, While NY is only in few it will only show NY, but if CALI comes in view it will show the marker in Cali, as well if it was two show Louisiana in view.
Comment 5 written by Raymond Camden on 11 April 2010, at 10:03 AM
I'm rereading your comment and I think that may not _exactly_ match what you mean. Obviously if you set a marker to show at a 'low' zoom, then you would only see the CA one if you were zoomed in low enough. You can also notice when the view port changes. You could say, "If I'm in a range of these long/lat, I'm over CA so show marker X." Not sure how efficient it would be.