Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I had the same need and also could not find a good ready made solution so I coded and commented a test library below which can serve as the basis for any like project.
- Rather than rely on a single provider I implemented the same functionality in Flash across Bing Maps V7 (http://msdn.microsoft.com/en-us/library/gg427610.aspx), Google Maps V3 (https://developers.google.com/maps/documentation/javascript/) and MapQuest Maps V7 (http://developer.mapquest.com/web/products/featured/javascript) JavaScript maps. The code creates lines, dynamic markers and shadows, etc. and also calls their elevation apis. The sample app creates two of each map type in one html page.
- To do this you need to have Flash and JavaScript call each other with data and/or closure via registering functions which have names unique to that item.
- First each map has a unique mapId and each map MXML component registers a set of external methods with their unique mapId (e.g. fname_mapId) and then creates a Flex-iFrame with a parameterized src HTML for one of the three map types passing mapId, application name, start position etc.
- The HTML reads these parameters and gets the SWF object from the parent frame via the application name. It creates a map and registers listeners with the map and adds callbacks to the parent frame using its mapId (e.g. fname_mapId) that allow the creation of markers and lines, each of those with a unique id and again registering functions with the parent frame that now also include the marker-or-line id in their name (e.g. fname_mapId_[lineId|markerId]) that allow setting icons, shadows, line color and their position change or removal.
- The HTML also registers functions by mapId to check elevation of a position or a path in general. These call the ajax or routine to request the elevation(s), calling back to the JavaScript and then back to the AS3 registered functions.
- -C
- Logic in the test actions & callbacks - designed to test all the implemented functionality:
- Double click on map:
- Create pin:
- - Set to default name
- - Add to map
- - Set icon and shadow (note Bing does not take shadows; Google can take a marker mask for clicking - not implemented)
- - Call to map for elevation
- Remove previous marker
- Log current markers (should be only 1)
- Create black line from start to pin:
- - Change line color to blue
- - Call to map for path elevation
- Remove previous line
- Log current lines (should be only 1)
- Drag marker:
- Start:
- - only logs it was called
- Drag:
- - only logs it was called
- End:
- - Move line to new lat lng
- - Set line color to orange
- - Sets map to not accept double clicks
- Double click marker:
- - Sets map to accept double clicks again (so after you drag you need to double click a marker to have map accept double clicks again)
- Elevation callback:
- - Changes pin name to include elevation in name
- - Sets new icon and shadow for marker
- Elevation path callback - set to 300m & 500m - is balloon at 500m on path going to hit? - error if elevation of path >=500, warn if >=300
- - Sets line color if path max elevation as sampled is:
- Purple: elevation request error flag set
- Red: at or above error level
- Red-Yellow: at or above warning but requested distance not met
- Yellow: at or above warning level
- Yellow-Green: below warning level but requested distance not met
- Green: below warning level
- Dependencies: Base64 (http://www.foxarc.com/blog/article/60.htm), Flex-iFrame (https://github.com/flex-users/flex-iframe) (modify and set overflow:none to get rid of Chrome scrollbars), jQuery (http://jquery.com/)
- TestFloidJavaScriptMaps.mxml - Flash application - creates 2x3 maps:
- <?xml version="1.0" encoding="utf-8"?>
- <!-- faigelabs - test code for Floid maps - cfaigle - free -->
- <s:Application xmlns:fx = "http://ns.adobe.com/mxml/2009"
- xmlns:s = "library://ns.adobe.com/flex/spark"
- xmlns:mx = "library://ns.adobe.com/flex/mx"
- xmlns:flexiframe = "http://code.google.com/p/flex-iframe/"
- minWidth = "955"
- minHeight = "600"
- creationComplete = "creationCompleteHandler(event)"
- xmlns:local = "*"
- xmlns:maps = "com.faiglelabs.floid.maps.*">
- <fx:Script>
- <![CDATA[
- import mx.events.FlexEvent;
- protected function creationCompleteHandler(event:FlexEvent):void
- {
- // Put stuff here to do when application is created...
- }
- ]]>
- </fx:Script>
- <s:layout>
- <s:VerticalLayout/>
- </s:layout>
- <s:HGroup>
- <s:VGroup>
- <s:TextArea fontSize="14" width="400" height="24" borderVisible="false" text="Google"/>
- <maps:FloidGoogleMap id="floidGoogleMap_1" mapId="1" height="400" width="400" latStart="39.885522" lngStart="3.096644"/>
- </s:VGroup>
- <s:VGroup>
- <s:TextArea fontSize="14" width="400" height="24" borderVisible="false" text="MapQuest"/>
- <maps:FloidMapQuestMap id="floidMapQuestMap_2" mapId="2" height="400" width="400" latStart="39.885522" lngStart="3.096644"/>
- </s:VGroup>
- <s:VGroup>
- <s:TextArea fontSize="14" width="400" height="24" borderVisible="false" text="Bing"/>
- <maps:FloidBingMap id="floidBingMap_3" mapId="3" height="400" width="400" latStart="39.885522" lngStart="3.096644"/>
- </s:VGroup>
- </s:HGroup>
- <s:HGroup>
- <s:VGroup>
- <s:TextArea fontSize="14" width="400" height="24" borderVisible="false" text="Google"/>
- <maps:FloidGoogleMap id="floidGoogleMap_4" mapId="4" height="400" width="400" latStart="39.885522" lngStart="3.096644"/>
- </s:VGroup>
- <s:VGroup>
- <s:TextArea fontSize="14" width="400" height="24" borderVisible="false" text="MapQuest"/>
- <maps:FloidMapQuestMap id="floidMapQuestMap_5" mapId="5" height="400" width="400" latStart="39.885522" lngStart="3.096644"/>
- </s:VGroup>
- <s:VGroup>
- <s:TextArea fontSize="14" width="400" height="24" borderVisible="false" text="Bing"/>
- <maps:FloidBingMap id="floidBingMap_6" mapId="6" height="400" width="400" latStart="39.885522" lngStart="3.096644"/>
- </s:VGroup>
- </s:HGroup>
- </s:Application>
- FloidMap.mxml - Map component:
- <?xml version="1.0" encoding="utf-8"?>
- <!-- faigelabs - test code for Floid maps - cfaigle - free -->
- <s:Group xmlns:fx = "http://ns.adobe.com/mxml/2009"
- xmlns:s = "library://ns.adobe.com/flex/spark"
- xmlns:mx = "library://ns.adobe.com/flex/mx"
- width = "400"
- height = "400"
- creationComplete = "creationCompleteHandler(event)">
- <fx:Script>
- <![CDATA[
- import mx.events.FlexEvent;
- import com.google.code.flexiframe.IFrame;
- import com.faiglelabs.floid.testcode.FloidPinAlt;
- public var applicationName : String = "TestFloidJavaScriptMaps"; // The name to pass to the html frame to find the swf object
- public var mapId : int = 0;
- public var mapType : String = "NONE"; // No support for abstract classes in AS3
- public var latStart : Number = 39.885522; // Flat low area in ocean by Mallorca near mountains - decimal degrees
- public var lngStart : Number = 3.096644; // for line elevation color testing
- protected var floidMapFlexIFrame : IFrame = null;
- protected var nextMarkerId : int = 1;
- protected var nextLineId : int = 1;
- protected var currentMarkers : Object = new Object(); // This holds a reference by markerId to its pin
- protected var currentLines : Object = new Object(); // This just holds a reference to its own lineId
- public var testMaxMarkers : int = 50; // We do not want to flood the elevation service
- // Note: Bing says 1024 max here: http://msdn.microsoft.com/en-us/library/jj158961.aspx
- // but seems to stop responding with even less than this 50 total
- public var testRequestedDistance : int = 400; // We would like them spaced 400 meters apart = 20km total
- public var testErrorElevation : Number = 500; // In meters - above or equal is error
- public var testWarningElevation : Number = 300; // In meters - below this is ok, otherwise warning
- // Component is created:
- protected function creationCompleteHandler(event:FlexEvent):void
- {
- // Put stuff here you want to do when the component is initialized...
- trace("FloidMap Component initialized: [" + mapType + "]=" + mapId);
- // First, add our callbacks to the DOM now that we know our mapId and before the map is created:
- ExternalInterface.addCallback("floidMapInitialized_" + mapId, floidMapInitialized);
- ExternalInterface.addCallback("floidMapFrameUnloaded_" + mapId, floidMapFrameUnloaded);
- ExternalInterface.addCallback("floidMapDoubleClick_" + mapId, floidMapDoubleClick);
- ExternalInterface.addCallback("floidMapPathMaxElevationCallBack_" + mapId, floidMapPathMaxElevationCallBack);
- // Create the IFrame that loads the maps html:
- floidMapFlexIFrame = new IFrame("floidMapFrame_" + mapId);
- floidMapFlexIFrame.percentHeight = 100;
- floidMapFlexIFrame.percentWidth = 100;
- floidMapFlexIFrame.source = "Floid"+mapType+"MapFrame.html?mapId=" + mapId + "&obj="+applicationName+"&latStart="+latStart+"&lngStart="+lngStart;
- floidMapFlexIFrame.overlayDetection = true;
- floidMapFlexIFrame.scrollPolicy = IFrame.SCROLL_POLICY_OFF;
- floidMapFlexIFrame.addEventListener("frameLoad", floidMapFrameLoad);
- this.addElement(floidMapFlexIFrame);
- trace("FloidMap Component initialized: [" + mapType + "]=" + mapId + " completed.");
- }
- // Distance functions:
- // -------------------
- protected var rad :Object = function(x:*):Number {return x*Math.PI/180;}
- // Meters:
- protected function distanceHaversine(startLat:Number, startLng:Number, endLat:Number, endLng:Number):Number
- {
- var R : int = 6371; // earth's mean radius in km
- var dLat : Number = rad(endLat - startLat);
- var dLong : Number = rad(endLng - startLng);
- var a : Number = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(rad(startLat)) * Math.cos(rad(endLat)) * Math.sin(dLong/2) * Math.sin(dLong/2);
- var c : Number = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
- var d : Number = R * c;
- return d*1000; // km to m
- }
- // Map functions & callbacks:
- // --------------------------
- protected function floidMapInitialized():void
- {
- // Put stuff here you want to do when the map is initialized...
- trace("FloidMap initialized: [" + mapType + "]=" + mapId);
- }
- protected function floidMapFrameLoad(event:Event):void
- {
- // Put stuff here you want to do when the frame is loaded
- trace("FloidMap frame loaded: [" + mapType + "]=" + mapId);
- }
- protected function floidMapFrameUnloaded():void
- {
- // Put stuff here you want to do when the underlying frame receives an unload
- trace("map frame unloaded: " + mapId);
- }
- protected function floidMapDoubleClick(lat:Number, lng:Number):void
- {
- trace("floidmapDoubleClick: " + mapId + " lat: " + lat + " lng: " + lng);
- var markerId : Number = nextMarkerId++;
- trace("create new map marker: " + markerId);
- trace("calling external interface: floidMapCreateNewMarker_" + mapId + " lat: " + lat + " lng: " + lng);
- ExternalInterface.call("floidMapCreateNewMarker_" + mapId, lat, lng, markerId);
- // Add callback handlers for this marker:
- ExternalInterface.addCallback("floidMapMarkerDoubleClick_" + mapId + "_" + markerId, floidMapMarkerDoubleClick);
- ExternalInterface.addCallback("floidMapMarkerDragStart_" + mapId + "_" + markerId, floidMapMarkerDragStart);
- ExternalInterface.addCallback("floidMapMarkerDragEnd_" + mapId + "_" + markerId, floidMapMarkerDragEnd);
- ExternalInterface.addCallback("floidMapMarkerDrag_" + mapId + "_" + markerId, floidMapMarkerDrag);
- ExternalInterface.addCallback("floidMapMarkerElevationResult_" + mapId + "_" + markerId, floidMapMarkerElevationResult);
- // The rest of ths routine is test code:
- // =====================================
- // Make a test call to create a marker icon for this:
- var floidPinAlt : FloidPinAlt = new FloidPinAlt();
- floidPinAlt.pinName = "Floid-"+mapId+"-"+markerId;
- currentMarkers[markerId] = floidPinAlt;
- var floidPinMarkerIconBase64String : String = floidPinAlt.makeIcon();
- trace("Setting marker icon: " + mapId + " " + markerId);
- trace("marker string: " + floidPinMarkerIconBase64String);
- ExternalInterface.call("floidMapMarkerSetIconString_" + mapId + "_" + markerId, floidPinMarkerIconBase64String);
- var floidPinMarkerIconShadowBase64String : String = floidPinAlt.makeIconShadow();
- trace("Setting marker shadow: " + mapId + " " + markerId);
- trace("marker shadow string: " + floidPinMarkerIconShadowBase64String);
- ExternalInterface.call("floidMapMarkerSetIconShadowString_" + mapId + "_" + markerId, floidPinMarkerIconShadowBase64String);
- // TEST CODE BELOW - remove previous marker - so we only have one on map at a time
- if(markerId >1)
- {
- trace("Removing previous marker: " + (markerId-1));
- var removePreviousMarkerCallName : String = "floidMapRemoveMarker_" + mapId + "_" + (markerId-1);
- ExternalInterface.call(removePreviousMarkerCallName);
- // Remove the marker from our associative array:
- delete currentMarkers[markerId-1];
- }
- // TEST CODE BELOW - add a line from latStart, lngStart to this point:
- var lineId : Number = nextLineId++;
- trace("create new map line: " + lineId);
- ExternalInterface.call("floidMapCreateNewLine_" + mapId, lineId, latStart, lngStart, lat, lng, 255, 0, 0, 0, 2); // Opaque Black 2 px line
- currentLines[lineId] = lineId;
- // TEST CODE BELOW - change the line to blue:
- trace("Setting line color to blue:" + lineId);
- ExternalInterface.call("floidMapSetLineColor_" + mapId + "_" + lineId, 255, 0, 0, 255, 2);
- // TEST CODE BELOW - check the line to see if its height is ok
- trace("Checking maxPathElevation:" + lineId);
- floidMapGetPathMaxElevation(lineId, latStart, lngStart, lat, lng, testMaxMarkers, testRequestedDistance, testErrorElevation, testWarningElevation);
- // TEST CODE BELOW - remove previous line
- if(lineId >1)
- {
- trace("Removing previous line: " + (lineId-1));
- var removePreviousLineCallName : String = "floidMapRemoveLine_" + mapId + "_" + (lineId-1);
- ExternalInterface.call(removePreviousLineCallName);
- // Remove the line from our associative array:
- delete currentLines[lineId-1];
- }
- // TEST CODE BELOW - get the elevation for the pin:
- trace("Call back and requesting Elevation: " + mapId + " " + markerId + " " + lat + " " + lng);
- ExternalInterface.call("floidMapMarkerRequestElevation_" + mapId + "_" + markerId, markerId, lat, lng);
- }
- // Marker double click:
- protected function floidMapMarkerDoubleClick(markerId:Number):void
- {
- trace("floidMapMarkerDoubleClick: " + markerId);
- // TEST CODE BELOW - RETURN THE MAP TO ACCEPTING DOUBLE CLICKS AFTER DOUBLE CLICKING ANY MARKER:
- trace("Turn the map to accept double clicks after double clicking any marker: " + mapId);
- ExternalInterface.call("floidMapAddDoubleClickListener_" + mapId, mapId);
- }
- // Marker drag start:
- protected function floidMapMarkerDragStart(markerId:Number, lat:Number, lng:Number):void
- {
- trace("floidMapMarkerDragStart: " + markerId + " " + lat + " " + lng);
- }
- // Marker drag end:
- protected function floidMapMarkerDragEnd(markerId:Number, lat:Number, lng:Number):void
- {
- trace("floidMapMarkerDragEnd: " + markerId + " " + lat + " " + lng);
- // TEST CODE BELOW:
- trace("Call back and requesting Elevation: " + mapId + " " + markerId + " " + lat + " " + lng);
- ExternalInterface.call("floidMapMarkerRequestElevation_" + mapId + "_" + markerId, markerId, lat, lng);
- // Choose only one of the following two tests or you won't be able to click marker to reset map to accepting double clicks:
- // // TEST CODE BELOW - TURN THE MARKER TO NOT CLICKABLE AFTER DRAGGING:
- // trace("Turn the marker to not clickable after dragging: " + mapId);
- // ExternalInterface.call("floidMapMarkerSetClickable_" + mapId + "_" + markerId, false);
- // This is the other one:
- // TEST CODE BELOW - TURN THE MAP TO NOT ACCEPT DOUBLE CLICKS AFTER DRAGGING:
- trace("Turn the map to not accept double clicks after dragging: " + mapId);
- ExternalInterface.call("floidMapRemoveDoubleClickListener_" + mapId, mapId);
- // TEST CODE BELOW - SET THE POLYLINE TO MOVE TO THE NEW POSITION
- trace("Moving the last polyline to this new position.");
- // Note: The test code adds markers and lines 1-1 and only keeps one marker on map, which is why this works:
- ExternalInterface.call("floidMapMoveLine__" + mapId + "_" + (nextLineId - 1), latStart , lngStart, lat, lng);
- // TEST CODE BELOW - change the line to orange:
- trace("Setting line color to orange:" + (nextLineId - 1));
- ExternalInterface.call("floidMapSetLineColor_" + mapId + "_" + (nextLineId - 1), 255, 255, 127, 0, 2);
- // TEST CODE BELOW - check the line to see if its height is ok
- trace("Checking maxPathElevation:" + (nextLineId - 1)); // lineId sl sl el el ms rpdft errft warnft
- floidMapGetPathMaxElevation((nextLineId - 1), latStart, lngStart, lat, lng, testMaxMarkers, testRequestedDistance, testErrorElevation, testWarningElevation);
- // TEST CODE BELOW - PRINT OUT THE CURRENT MARKER LIST:
- trace("Current marker list: " + mapId);
- for (var markerIdString:String in currentMarkers)
- {
- trace("currentMarkers[" + markerIdString + "] = " + currentMarkers[markerIdString] + ";");
- }
- // TEST CODE BELOW - PRINT OUT THE CURRENT LINE LIST:
- trace("Current line list: " + mapId);
- for (var lineIdString:String in currentLines)
- {
- trace("currentLines[" + lineIdString + "] = " + currentLines[lineIdString]);
- }
- }
- // Marker drag:
- protected function floidMapMarkerDrag(markerId:Number, lat:Number, lng:Number):void
- {
- trace("floidMapMarkerDrag: " + markerId + " " + lat + " " + lng);
- }
- // Marker elevation result:
- protected function floidMapMarkerElevationResult(markerId:Number, elevation: Number):void
- {
- trace("floidMapMarkerElevationResult: " + markerId + " " + elevation);
- // TEST CODE BELOW HERE - CHANGE THE ICON NAME TO INCLUDE THE STRING AND RE-SEND THE ICON AND SHADOW:
- var floidPinAlt:FloidPinAlt = FloidPinAlt(currentMarkers[markerId]);
- trace("floidMapMarkerElevationResult: " + markerId + " " + elevation + " retrieved marker:");
- floidPinAlt.pinName = "Floid-"+mapId+"-"+markerId + " [" + int(elevation) + "]";
- var floidPinMarkerIconBase64String : String = floidPinAlt.makeIcon();
- trace("Setting marker icon: " + mapId + " " + markerId);
- trace("marker string: " + floidPinMarkerIconBase64String);
- ExternalInterface.call("floidMapMarkerSetIconString_" + mapId + "_" + markerId, floidPinMarkerIconBase64String);
- var floidPinMarkerIconShadowBase64String : String = floidPinAlt.makeIconShadow();
- trace("Setting marker shadow: " + mapId + " " + markerId);
- trace("marker shadow string: " + floidPinMarkerIconShadowBase64String);
- }
- // Get maximum path elevation:
- protected function floidMapGetPathMaxElevation(lineId:int, startLat:Number, startLng:Number, endLat:Number, endLng:Number, maxSamples:int, requestedPathDistanceInMeters:Number, errorElevation:Number, warningElevation:Number):void
- {
- // Calculate the max actual max number of points:
- var distanceBetween : Number = distanceHaversine(startLat, startLng, endLat, endLng);
- var requiredSamples : int = Math.ceil(distanceBetween / requestedPathDistanceInMeters) + 1; // Note that for 10 evenly spaced slots we need 11 points because we always check both start and end
- var actualSamples : int = Math.min(maxSamples, requiredSamples);
- var actualPathDistanceInMeters : Number = distanceBetween / (actualSamples-1); // See note above - this is the opposite part...
- trace("floidMapGetPathMaxElevation: [" + startLat + ", " + startLng + "] -> [" + endLat + ", " + "] (" + distanceBetween + "m) samples: max " + maxSamples + " actual " + actualSamples + " dist: req " + requestedPathDistanceInMeters + " actual: " + actualPathDistanceInMeters + " level: err " + errorElevation + " warn " + warningElevation);
- ExternalInterface.call("floidMapGetPathMaxElevation_" + mapId, lineId, startLat, startLng, endLat, endLng, maxSamples, requestedPathDistanceInMeters, errorElevation, warningElevation, actualSamples, actualPathDistanceInMeters);
- }
- // Callback for GetPathMaxElevation call:
- protected function floidMapPathMaxElevationCallBack(lineId:int, startLat:Number, startLng:Number, endLat:Number, endLng:Number, maxSamples:int, requestedPathDistanceInMeters:Number, errorElevation:Number, warningElevation:Number, maxElevation:Number, actualSamples:int, actualPathDistanceInMeters:Number, errorFlag:Boolean):void
- {
- trace("floidMapPathMaxElevation Callback: maxElevation " + maxElevation + " errorFlag " + errorFlag);
- if(errorFlag)
- {
- floidMapSetLineColor(lineId, 255, 255, 70, 255, 2); // Warning color - magenta
- trace("Could not check path");
- }
- else
- {
- if(maxElevation > errorElevation)
- {
- // Elevation is at error:
- floidMapSetLineColor(lineId, 255, 255, 70, 70, 2); // Error color - red
- trace("Path Elevation Below Error Elevation: " + maxElevation + "m vs " + errorElevation + "m");
- }
- else if(maxElevation > warningElevation)
- {
- // Elevation was good - how did we check:
- if(actualPathDistanceInMeters > requestedPathDistanceInMeters)
- {
- // Do not have enough info - warning
- floidMapSetLineColor(lineId, 255, 255, 200, 128, 2); // Warning color - red-yellow
- trace("Path was at warning but not checked to desired distance: " + actualPathDistanceInMeters + "m vs " + requestedPathDistanceInMeters + "m");
- }
- else
- {
- // Elevation is at warning:
- floidMapSetLineColor(lineId, 255, 255, 255, 70, 2); // Warning color - yellow
- trace("Path Elevation Below Warning Elevation: " + maxElevation + "m vs " + warningElevation + "m");
- }
- }
- else
- {
- // Elevation was good - how did we check:
- if(actualPathDistanceInMeters > requestedPathDistanceInMeters)
- {
- // Do not have enough info - warning
- floidMapSetLineColor(lineId, 255, 200, 255, 128, 2); // Warning color - yellow - green
- trace("Path was good but not checked to desired distance: " + actualPathDistanceInMeters + "m vs " + requestedPathDistanceInMeters + "m");
- }
- else
- {
- // All was good...
- floidMapSetLineColor(lineId, 255, 70, 255, 70, 2); // Good color - green
- trace("Elevation was good!");
- }
- }
- }
- // Put stuff here, like removing this from a list of path items we are waiting for to have a full mission check complete
- // Note some elevation apis as implemented in $.getJSON fail silently and should be redone using $.ajax
- trace("mapQuestMapsPathMaxElevation Callback complete");
- }
- protected function floidMapSetLineColor(lineId:Number, a:int, r:int, g:int, b:int, w:int):void
- {
- ExternalInterface.call("floidMapSetLineColor_" + mapId + "_" + lineId, a, r, g, b, w);
- }
- ]]>
- </fx:Script>
- </s:Group>
- FloidBingMap.mxml - Bing map component:
- <?xml version="1.0" encoding="utf-8"?>
- <!-- faigelabs - test code for Floid maps - cfaigle - free -->
- <maps:FloidMap xmlns:fx = "http://ns.adobe.com/mxml/2009"
- xmlns:s = "library://ns.adobe.com/flex/spark"
- xmlns:mx = "library://ns.adobe.com/flex/mx"
- xmlns:maps = "com.faiglelabs.floid.maps.*"
- width = "400"
- height = "400">
- <fx:Script>
- <![CDATA[
- import mx.events.FlexEvent;
- // Component is created:
- override protected function creationCompleteHandler(event:FlexEvent):void
- {
- mapType = "Bing";
- super.creationCompleteHandler(event);
- }
- // Override and/or add functionality specific to this map type here...
- ]]>
- </fx:Script>
- </maps:FloidMap>
- FloidGoogleMap.mxml - Google map component:
- <?xml version="1.0" encoding="utf-8"?>
- <!-- faigelabs - test code for Floid maps - cfaigle - free -->
- <maps:FloidMap xmlns:fx = "http://ns.adobe.com/mxml/2009"
- xmlns:s = "library://ns.adobe.com/flex/spark"
- xmlns:mx = "library://ns.adobe.com/flex/mx"
- xmlns:maps = "com.faiglelabs.floid.maps.*"
- width = "400"
- height = "400">
- <fx:Script>
- <![CDATA[
- import mx.events.FlexEvent;
- // Component is created:
- override protected function creationCompleteHandler(event:FlexEvent):void
- {
- mapType = "Google";
- super.creationCompleteHandler(event);
- }
- // Override and/or add functionality specific to this map type here...
- ]]>
- </fx:Script>
- </maps:FloidMap>
- FloidMapQuestMap.mxml - MapQuest map component:
- <?xml version="1.0" encoding="utf-8"?>
- <!-- faigelabs - test code for Floid maps - cfaigle - free -->
- <maps:FloidMap xmlns:fx = "http://ns.adobe.com/mxml/2009"
- xmlns:s = "library://ns.adobe.com/flex/spark"
- xmlns:mx = "library://ns.adobe.com/flex/mx"
- xmlns:maps = "com.faiglelabs.floid.maps.*"
- width = "400"
- height = "400">
- <fx:Script>
- <![CDATA[
- import mx.events.FlexEvent;
- // Component is created:
- override protected function creationCompleteHandler(event:FlexEvent):void
- {
- mapType = "MapQuest";
- super.creationCompleteHandler(event);
- }
- // Override and/or add functionality specific to this map type here...
- ]]>
- </fx:Script>
- </maps:FloidMap>
- FloidBingMapFrame.html - Bing html component:
- <!DOCTYPE html>
- <html>
- <head>
- <!-- faigelabs - test code for Floid maps - cfaigle - free -->
- <meta name="viewport"
- content="initial-scale=1.0, user-scalable=no" />
- <style type="text/css">
- html { height: 100% }
- body { height: 100%; margin: 0; padding: 0 }
- #map_canvas { height: 100% }
- </style>
- <script type ="text/javascript"
- src ="FloidMapUtilities.js">
- </script>
- <script type ="text/javascript"
- src ="jquery-1.9.1.min.js">
- </script>
- <script type ="text/javascript"
- src ="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0">
- </script>
- <script type="text/javascript">
- ///////////////////////////////////////////
- ///////////////////////////////////////////
- // Main Initialize - Magic starts here: //
- ///////////////////////////////////////////
- ///////////////////////////////////////////
- function initialize()
- {
- //------------------------------------------------------------------------------------//
- // We need to keep track of the map listeners so we can turn them on/off in routines //
- //------------------------------------------------------------------------------------//
- var mapDoubleClickListener = null;
- //----------------------------------//
- // Create the default map options: //
- //----------------------------------//
- var mapOptions = {
- credentials: "YOUR_API_KEY",
- center: new Microsoft.Maps.Location(latStart, lngStart),
- mapTypeId: Microsoft.Maps.MapTypeId.road,
- zoom: 12
- };
- //---------------------------------------//
- // Create the map: //
- //---------------------------------------//
- var map = new Microsoft.Maps.Map(document.getElementById("map_canvas"), mapOptions);
- //----------------------------------------------//
- // By default we add the double click listener: //
- //----------------------------------------------//
- addDoubleClickListener();
- //-------------------------------------------------------//
- // Make add and remove map double click event listeners: //
- //-------------------------------------------------------//
- function addDoubleClickListener()
- {
- log("Adding map double click listener: " + mapId);
- // Remove previous
- if(mapDoubleClickListener != null)
- {
- Microsoft.Maps.Events.removeHandler(mapDoubleClickListener);
- mapDoubleClickListener = null;
- }
- // Add new:
- try
- {
- mapDoubleClickListener = Microsoft.Maps.Events.addHandler(map,
- 'dblclick',
- function(event)
- {
- if(event.target==map)
- {
- var point = new Microsoft.Maps.Point(event.getX(), event.getY());
- var loc = event.target.tryPixelToLocation(point);
- mapDoubleClick(loc);
- event.handled = true;
- }
- });
- }
- catch(err)
- {
- log("addDoubleClickListener: " + err);
- mapDoubleClickListener = null;
- }
- }
- function removeDoubleClickListener()
- {
- log("Removing map double click listener: " + mapId);
- // Remove previous
- if(mapDoubleClickListener != null)
- {
- Microsoft.Maps.Events.removeHandler(mapDoubleClickListener);
- mapDoubleClickListener = null;
- }
- // Add new:
- try
- {
- mapDoubleClickListener = Microsoft.Maps.Events.addHandler(map,
- 'dblclick',
- function(event)
- {
- if(event.target==map)
- {
- event.handled = true;
- }
- });
- }
- catch(err)
- {
- log("removeDoubleClickListener: " + err);
- mapDoubleClickListener = null;
- }
- }
- //------------------------------------------//
- // Create handlers for map double click: //
- //------------------------------------------//
- // mapDoubleClick:
- function mapDoubleClick(latLng)
- {
- log("mapDoubleClick: " + latLng);
- log("calling flash routine: " + "floidMapDoubleClick_" + mapId);
- swfObject["floidMapDoubleClick_" + mapId](latLng.latitude, latLng.longitude);
- }
- //********************************//
- //********************************//
- // Calls from Flash to the map: //
- //********************************//
- //********************************//
- ////////////////////////////////////////////////
- // Function creators for flash calls to map: //
- ////////////////////////////////////////////////
- // -----------------------------
- // floidBingMapsCreateNewMarker:
- // -----------------------------
- function makeFloidBingMapsCreateNewMarkerFunction()
- {
- return function(lat, lng, markerId)
- {
- log("floidBingMapsCreateNewMarker: " + lat + " " + lng + " " + markerId);
- createNewMarker(lat, lng, markerId);
- }
- }
- // ---------------------------
- // floidBingMapsCreateNewLine:
- // ---------------------------
- function makeFloidBingMapsCreateNewLineFunction()
- {
- return function(lineId, startLat, startLng, endLat, endLng, a, r, g, b, w)
- {
- log("floidBingMapsCreateNewLine: " + startLat + " " + startLng + " " + endLat + " " + endLng + " " + a + " " + r + " " + g + " " + b);
- createNewLine(lineId, startLat, startLng, endLat, endLng, a, r, g, b, w);
- }
- }
- // ------------------------------------
- // floidBingMapsAddDoubleClickListener:
- // ------------------------------------
- function makeFloidBingMapsAddDoubleClickListenerFunction()
- {
- return function(mapId)
- {
- log("floidBingMapsAddDoubleClickListener: " + mapId);
- addDoubleClickListener();
- }
- }
- // ---------------------------------------
- // floidBingMapsRemoveDoubleClickListener:
- // ---------------------------------------
- function makeFloidBingMapsRemoveDoubleClickListenerFunction()
- {
- return function(mapId)
- {
- log("floidBingMapsRemoveDoubleClickListener: " + mapId);
- removeDoubleClickListener();
- }
- }
- // ----------------------------------
- // floidBingMapsGetPathMaxElevation:
- // ----------------------------------
- function makeFloidBingMapsGetPathMaxElevationFunction()
- {
- return function(lineId, startLat, startLng, endLat, endLng, maxMarkers, requestedPathDistanceInMeters, errorElevation, warningElevation, actualMarkers, actualPathDistanceInMeters)
- {
- log("floidBingMapsGetPathMaxElevation: " + startLat + " " + startLng + " " + endLat + " " + endLng + " " + maxMarkers + " " + requestedPathDistanceInMeters + " " + errorElevation + " " + warningElevation + " " + actualMarkers + " " + actualPathDistanceInMeters);
- getPathMaxElevation(lineId, startLat, startLng, endLat, endLng, maxMarkers, requestedPathDistanceInMeters, errorElevation, warningElevation, actualMarkers, actualPathDistanceInMeters);
- }
- }
- //////////////////////////////////////////////////////////////////////////////
- // Make a set of function creation routines for the flash calls to the DOM: //
- //////////////////////////////////////////////////////////////////////////////
- // -----------------------------
- // floidBingMapsCreateNewMarker:
- // -----------------------------
- var floidBingMapsCreateNewMarkerFunctionName = "floidMapCreateNewMarker_" + mapId;
- this.parent[floidBingMapsCreateNewMarkerFunctionName] = makeFloidBingMapsCreateNewMarkerFunction();
- // ---------------------------
- // floidBingMapsCreateNewLine:
- // ---------------------------
- var floidBingMapsCreateNewLineFunctionName = "floidMapCreateNewLine_" + mapId;
- this.parent[floidBingMapsCreateNewLineFunctionName] = makeFloidBingMapsCreateNewLineFunction();
- // ------------------------------------
- // floidBingMapsAddDoubleClickListener:
- // ------------------------------------
- var floidBingMapsAddDoubleClickListenerFunctionName = "floidMapAddDoubleClickListener_" + mapId;
- this.parent[floidBingMapsAddDoubleClickListenerFunctionName] = makeFloidBingMapsAddDoubleClickListenerFunction();
- // ---------------------------------------
- // floidBingMapsRemoveDoubleClickListener:
- // ---------------------------------------
- var floidBingMapsRemoveDoubleClickListenerFunctionName = "floidMapRemoveDoubleClickListener_" + mapId;
- this.parent[floidBingMapsRemoveDoubleClickListenerFunctionName] = makeFloidBingMapsRemoveDoubleClickListenerFunction();
- // --------------------------------
- // floidBingMapsGetPathMaxElevation:
- // --------------------------------
- var floidBingMapsGetPathMaxElevationFunctionName = "floidMapGetPathMaxElevation_" + mapId;
- this.parent[floidBingMapsGetPathMaxElevationFunctionName] = makeFloidBingMapsGetPathMaxElevationFunction();
- /////////////////////////////
- // Map routines: //
- /////////////////////////////
- var clickable = true;
- // ----- MARKERS -----
- // ----------------
- // createNewMarker:
- // ----------------
- function createNewMarker(lat, lng, markerId)
- {
- log("Making new marker [" + mapId + "] ("+lat + ", " + lng + ")");
- var markerOptions = {
- draggable: true
- };
- var marker = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(lat, lng), markerOptions);
- var dragStarted = false;
- var dragged = false;
- map.entities.push(marker);
- ////////////////////////////////////////////////
- // Marker listeners that call back to flash: //
- ////////////////////////////////////////////////
- // ------------------------------------------------------
- // OK, here we want to add the listeners for this marker:
- // ------------------------------------------------------
- Microsoft.Maps.Events.addHandler( marker,
- 'dblclick',
- function(event)
- {
- log("*************** MARKER DOUBLE CLICK ************");
- dragStarted = false;
- dragged = false;
- if(clickable)
- {
- swfObject["floidMapMarkerDoubleClick_" + mapId+"_"+markerId](markerId);
- }
- event.handled = true;
- });
- Microsoft.Maps.Events.addHandler( marker,
- 'dragstart',
- function(event)
- {
- dragStarted = true;
- dragged = false;
- });
- Microsoft.Maps.Events.addHandler( marker,
- 'dragend',
- function(event)
- {
- if(dragged)
- {
- swfObject["floidMapMarkerDragEnd_" + mapId+"_"+markerId](markerId, event.entity.getLocation().latitude, event.entity.getLocation().longitude);
- }
- dragStarted = false;
- dragged = false;
- });
- Microsoft.Maps.Events.addHandler( marker,
- 'drag',
- function(event)
- {
- if(dragStarted)
- {
- dragStarted = false;
- dragged = true;
- swfObject["floidMapMarkerDragStart_" + mapId+"_"+markerId](markerId, event.entity.getLocation().latitude, event.entity.getLocation().longitude);
- }
- swfObject["floidMapMarkerDrag_" + mapId+"_"+markerId](markerId, event.entity.getLocation().latitude, event.entity.getLocation().longitude);
- });
- //***********************************//
- //***********************************//
- // Calls from Flash to the marker: //
- //***********************************//
- //***********************************//
- ///////////////////////////////////////////////////
- // Function creators for flash calls to marker: //
- ///////////////////////////////////////////////////
- // -----------------------------------------------------
- // floidBingMapsMarkerSetClickable: [function creator]
- // -----------------------------------------------------
- function makeFloidBingMapsMarkerSetClickableFunction(marker, markerId)
- {
- return function(isClickable)
- {
- clickable = isClickable;
- }
- }
- // -----------------------------------------------------
- // floidBingMapsMarkerSetDraggable: [function creator]
- // -----------------------------------------------------
- function makeFloidBingMapsMarkerSetDraggableFunction(marker, markerId)
- {
- return function(draggable)
- {
- log("floidBingMapsMarkerSettingDraggable: " + markerId + " to " + draggable);
- marker.setOptions({draggable: draggable});
- }
- }
- // -----------------------------------------------------
- // floidBingMapsMarkerSetIconString: [function creator]
- // -----------------------------------------------------
- function makeFloidBingMapsMarkerSetIconStringFunction(marker, markerId)
- {
- return function(iconString)
- {
- log("floidBingMapsMarkerSetIconString: " + markerId);
- log("marker icon string: " + iconString);
- var markerIcon = {icon: 'data:image/png;base64,'+iconString,
- height: 38,
- width: 100,
- anchor: new Microsoft.Maps.Point(0,38)
- };
- log("floidBingMapsMarkerSetIconString: " + markerId + " - markerIconMade");
- marker.setOptions(markerIcon);
- log("floidBingMapsMarkerSetIconString: " + markerId + " - markerIconSet");
- }
- }
- // ------------------------------------------------------------
- // floidBingMapsMarkerSetIconShadowString: [function creator]
- // ------------------------------------------------------------
- function makeFloidBingMapsMarkerSetIconShadowStringFunction(marker, markerId)
- {
- return function(iconShadowString)
- {
- log("floidBingMapsMarkerSetIconShadowString: " + markerId);
- log("marker shadow string: [NOT SUPPORTED BY BING?]" + iconShadowString);
- }
- }
- // ---------------------------------------------------------
- // floidBingMapsMarkerRequestElevation: [function creator]
- // ---------------------------------------------------------
- function makeFloidBingMapsMarkerRequestElevationFunction(marker, markerId)
- {
- return function(markerId, lat, lng)
- {
- log("floidBingMapsMarkerRequestElevation: " + markerId + " - Requesting Elevation for: " + lat + " " + lng);
- // Locations parameter:
- var bingElevationUrl = "http://dev.virtualearth.net/REST/v1/Elevation/List?points="+lat+","+lng+"&key=YOUR_API_KEY&jsonp=?";
- $.getJSON(bingElevationUrl, function(data) {
- log("requestElevationFunction: " + data );
- if( (data != null)
- && (data.resourceSets != null)
- && (data.resourceSets.length >0)
- && (data.resourceSets[0].resources != null)
- && (data.resourceSets[0].resources.length >0)
- && (data.resourceSets[0].resources[0] != null)
- && (data.resourceSets[0].resources[0].elevations != null)
- && (data.resourceSets[0].resources[0].elevations.length >0) )
- {
- var elevation = data.resourceSets[0].resources[0].elevations[0];
- log("Elevation of point: " + elevation);
- // call back into flash :)
- swfObject["floidMapMarkerElevationResult_" + mapId+"_"+markerId](markerId, elevation);
- log("elevation callback done:");
- }
- else
- {
- log("Error in requestElevationFunction results");
- }
- });
- log("floidBingMapsMarkerRequestElevation: " + markerId + " - Elevation Requested");
- }
- }
- // ---------------------------------------------
- // floidBingMapsRemoveMarker: [function creator]
- // ---------------------------------------------
- function makeFloidBingMapsRemoveMarkerFunction(marker, markerId)
- {
- return function()
- {
- log("floidBingMapsRemoveMarker: " + mapId + " " + markerId);
- try
- {
- // Remove the marker:
- map.entities.remove(marker);
- marker = null;
- // Remove the globally bound functions:
- delete this.parent[floidBingMapsMarkerSetClickableFunctionName];
- delete this.parent[floidBingMapsMarkerSetDraggableFunctionName];
- delete this.parent[floidBingMapsMarkerRequestElevationFunctionName];
- delete this.parent[floidBingMapsMarkerSetIconStringFunctionName];
- delete this.parent[floidBingMapsMarkerSetIconShadowStringFunctionName];
- // Don't forget me!
- delete this.parent[floidBingMapsRemoveMarkerFunctionName];
- }
- catch(err)
- {
- log("floidBingMapsRemoveMarker: error " + mapId + " " + markerId + " " + err);
- }
- log("Done removing marker.");
- }
- }
- ///////////////////////////////////////////////////////
- // Add the flash calls for this marker to the DOM: //
- ///////////////////////////////////////////////////////
- // -----------------------------------------------------
- // floidBingMapsMarkerSetClickable:
- // -----------------------------------------------------
- var floidBingMapsMarkerSetClickableFunctionName = "floidMapMarkerSetClickable_" + mapId + "_" + markerId;
- this.parent[floidBingMapsMarkerSetClickableFunctionName] = makeFloidBingMapsMarkerSetClickableFunction(marker, markerId);
- // -----------------------------------------------------
- // floidBingMapsMarkerSetDraggable:
- // -----------------------------------------------------
- var floidBingMapsMarkerSetDraggableFunctionName = "floidMapMarkerSetDraggable_" + mapId + "_" + markerId;
- this.parent[floidBingMapsMarkerSetDraggableFunctionName] = makeFloidBingMapsMarkerSetDraggableFunction(marker, markerId);
- // -----------------------------------------------------
- // floidBingMapsMarkerRequestElevation:
- // -----------------------------------------------------
- var floidBingMapsMarkerRequestElevationFunctionName = "floidMapMarkerRequestElevation_" + mapId + "_" + markerId;
- this.parent[floidBingMapsMarkerRequestElevationFunctionName] = makeFloidBingMapsMarkerRequestElevationFunction(marker, markerId);
- // -----------------------------------------------------
- // floidBingMapsMarkerSetIconString:
- // -----------------------------------------------------
- var floidBingMapsMarkerSetIconStringFunctionName = "floidMapMarkerSetIconString_" + mapId + "_" + markerId;
- this.parent[floidBingMapsMarkerSetIconStringFunctionName] = makeFloidBingMapsMarkerSetIconStringFunction(marker, markerId);
- // -----------------------------------------------------
- // floidBingMapsMarkerSetIconShadowString:
- // -----------------------------------------------------
- var floidBingMapsMarkerSetIconShadowStringFunctionName = "floidMapMarkerSetIconShadowString_" + mapId + "_" + markerId;
- this.parent[floidBingMapsMarkerSetIconShadowStringFunctionName] = makeFloidBingMapsMarkerSetIconShadowStringFunction(marker, markerId);
- // ----------------------------
- // floidBingMapsRemoveMarker:
- // ----------------------------
- var floidBingMapsRemoveMarkerFunctionName = "floidMapRemoveMarker_" + mapId + "_" + markerId;
- this.parent[floidBingMapsRemoveMarkerFunctionName] = makeFloidBingMapsRemoveMarkerFunction(marker, markerId);
- }
- // ----- LINES -----
- // -----------------
- // createNewLine:
- // -----------------
- function createNewLine(lineId, startLat, startLng, endLat, endLng, a, r, g, b, w)
- {
- var options = { strokeColor : new Microsoft.Maps.Color(a, r, g, b),
- strokeThickness : 1 };
- var line = new Microsoft.Maps.Polyline( [ new Microsoft.Maps.Location(startLat, startLng),
- new Microsoft.Maps.Location(endLat, endLng)
- ],
- options
- );
- map.entities.push(line);
- //////////////////////////////////////////////
- // Line listeners that call back to flash: //
- //////////////////////////////////////////////
- // ----------------------------------------------------
- // OK, here we want to add the listeners for this line:
- // ----------------------------------------------------
- // Default is no listeners for this line:
- //***********************************//
- //***********************************//
- // Calls from Flash to the line: //
- //***********************************//
- //***********************************//
- /////////////////////////////////////////////////
- // Function creators for flash calls to line: //
- /////////////////////////////////////////////////
- // -----------------------------------------------
- // floidBingMapsRemoveLine: [function creator]
- // -----------------------------------------------
- function makeFloidBingMapsRemoveLineFunction(line, lineId)
- {
- return function()
- {
- log("Removing line.");
- try
- {
- // Remove the line:
- map.entities.remove(line);
- line = null;
- // Remove the globally bound functions:
- // - empty
- // Don't forget me!
- delete this.parent[floidBingMapsRemoveLineFunctionName];
- }
- catch(err)
- {
- log("floidBingMapsRemoveMarker: error " + err);
- }
- log("Done removing line.");
- }
- }
- // -----------------------------------------------
- // floidBingMapsSetLineColor: [function creator]
- // -----------------------------------------------
- function makeFloidBingMapsSetLineColorFunction(line, lineId)
- {
- return function(a, r, g, b, w)
- {
- log("Changing line color.");
- try
- {
- var strokeOptions = { strokeColor: new Microsoft.Maps.Color(a, r, g, b), strokeThickness:w};
- line.setOptions(strokeOptions);
- }
- catch(err)
- {
- log("floidBingMapsSetLineColor: error " + err);
- }
- log("Done setting line color.");
- }
- }
- // -----------------------------------------
- // floidBingMapsMoveLine: [function creator]
- // -----------------------------------------
- function makeFloidBingMapsMoveLineFunction(line, lineId)
- {
- return function(startLat, startLng, endLat, endLng)
- {
- log("Moving line [" + lineId + "] to: " + startLat + " " + startLng + " -> " + endLat + " " + endLng);
- try
- {
- line.setLocations( [ new Microsoft.Maps.Location(startLat, startLng),
- new Microsoft.Maps.Location(endLat, endLng)
- ]);
- }
- catch(err)
- {
- log("floidBingMapsMoveLine: error " + err);
- }
- log("Done moving line.");
- }
- }
- /////////////////////////////////////////////////////
- // Add the flash calls for this line to the DOM: //
- /////////////////////////////////////////////////////
- // -----------------------------------------------------
- // floidBingMapsRemoveLineFunction:
- // -----------------------------------------------------
- var floidBingMapsRemoveLineFunctionName = "floidMapRemoveLine_" + mapId + "_" + lineId;
- this.parent[floidBingMapsRemoveLineFunctionName] = makeFloidBingMapsRemoveLineFunction(line, lineId);
- // -----------------------------------------------------
- // floidBingMapsSetLineColorFunction:
- // -----------------------------------------------------
- var floidBingMapsSetLineColorFunctionName = "floidMapSetLineColor_" + mapId + "_" + lineId;
- this.parent[floidBingMapsSetLineColorFunctionName] = makeFloidBingMapsSetLineColorFunction(line, lineId);
- // -----------------------------------------------------
- // floidBingMapsMoveLineFunction:
- // -----------------------------------------------------
- var floidBingMapsMoveLineFunctionName = "floidMapMoveLine__" + mapId + "_" + lineId;
- this.parent[floidBingMapsMoveLineFunctionName] = makeFloidBingMapsMoveLineFunction(line, lineId);
- }
- // ------ Paths -------
- // --------------------
- // getPathMaxElevation:
- // --------------------
- function getPathMaxElevation(lineId, startLat, startLng, endLat, endLng, maxSamples, requestedPathDistanceInMeters, errorElevation, warningElevation, actualSamples, actualPathDistanceInMeters)
- {
- var sampleLocations = calculateSamples(startLat, startLng, endLat, endLng, actualSamples);
- var sampleLatLngPointsString = "";
- var firstLocation = true;
- for(var i=0; i<sampleLocations.length; ++i)
- {
- if(firstLocation)
- {
- firstLocation = false;
- }
- else
- {
- sampleLatLngPointsString = sampleLatLngPointsString + ", ";
- }
- sampleLatLngPointsString = sampleLatLngPointsString + sampleLocations[i];
- }
- var bingElevationUrl = "http://dev.virtualearth.net/REST/v1/Elevation/List?points="+sampleLatLngPointsString+"&key=YOU_API_KEY&jsonp=?";
- // TODO Add a timer here since this can fail silently or use jQuery $.ajax instead:
- $.getJSON(bingElevationUrl, function(data) {
- log( data );
- var errorFlag = true;
- var maxElevation = -100000; // Min value
- if( (data != null)
- && (data.resourceSets != null)
- && (data.resourceSets.length >0)
- && (data.resourceSets[0].resources != null)
- && (data.resourceSets[0].resources.length >0)
- && (data.resourceSets[0].resources[0] != null)
- && (data.resourceSets[0].resources[0].elevations != null)
- && (data.resourceSets[0].resources[0].elevations.length >0) )
- {
- maxElevation = data.resourceSets[0].resources[0].elevations[0];
- errorFlag = false;
- for(var i=0; i<data.resourceSets[0].resources[0].elevations.length; ++i)
- {
- var sampleElevation = data.resourceSets[0].resources[0].elevations[i];
- if(sampleElevation > maxElevation) maxElevation = sampleElevation;
- }
- log("Max elevation of path: " + maxElevation);
- // call back into flash :)
- swfObject["floidMapPathMaxElevationCallBack_" + mapId](lineId, startLat, startLng, endLat, endLng, maxSamples, requestedPathDistanceInMeters, errorElevation, warningElevation, maxElevation, actualSamples, actualPathDistanceInMeters, errorFlag);
- log("elevation callback done:");
- }
- else
- {
- swfObject["floidMapPathMaxElevationCallBack_" + mapId](lineId, startLat, startLng, endLat, endLng, maxSamples, requestedPathDistanceInMeters, errorElevation, warningElevation, maxElevation, actualSamples, actualPathDistanceInMeters, errorFlag);
- log("Error in getPathMaxElevation results");
- }
- });
- log("getMaxPathElevation: " + lineId + " - Elevation Requested");
- }
- // OK, the map is fully initialized - call back into flash:
- swfObject["floidMapInitialized_" + mapId]();
- }
- function finalize()
- {
- // Remove global map functions - TODO: remove remaining marker & line functions
- try
- {
- delete this.parent[floidBingMapsCreateNewMarkerFunctionName];
- delete this.parent[floidBingMapsCreateNewLineFunctionName];
- delete this.parent[floidBingMapsAddDoubleClickListenerFunctionName];
- delete this.parent[floidBingMapsRemoveDoubleClickListenerFunctionName];
- delete this.parent[floidBingMapsGetPathMaxElevationFunctionName];
- }
- catch(err)
- {
- log("Removing map methods error: " + err);
- }
- if(swfObject != null)
- {
- // call back into flash :)
- swfObject["floidMapFrameUnloaded_" + mapId]();
- }
- }
- </script>
- </head>
- <body onload="initialize()" onunload="finalize()">
- <div id="map_canvas" style="width:100%; height:100%"></div>
- </body>
- </html>
- FloidGoogleMapFrame.html - Google html component:
- <!DOCTYPE html>
- <html>
- <head>
- <!-- faigelabs - test code for Floid maps - cfaigle - free -->
- <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
- <style type="text/css">
- html { height: 100% }
- body { height: 100%; margin: 0; padding: 0 }
- #map_canvas { height: 100% }
- </style>
- <script type ="text/javascript"
- src ="FloidMapUtilities.js">
- </script>
- <script type="text/javascript"
- src="https://maps.googleapis.com/maps/api/js?sensor=false"><!-- src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&sensor=false"> -->
- </script>
- <script type="text/javascript">
- ///////////////////////////////////////////
- ///////////////////////////////////////////
- // Main Initialize - Magic starts here: //
- ///////////////////////////////////////////
- ///////////////////////////////////////////
- function initialize()
- {
- //------------------------------------------------------------------------------------//
- // We need to keep track of the map listeners so we can turn them on/off in routines //
- //------------------------------------------------------------------------------------//
- var mapDoubleClickListener = null;
- //----------------------------------//
- // Create the default map options: //
- //----------------------------------//
- var mapOptions = {
- center : new google.maps.LatLng(latStart, lngStart),
- zoom : 12,
- mapTypeId : google.maps.MapTypeId.ROADMAP,
- disableDoubleClickZoom : true,
- disableDefaultUI : true,
- panControl : true,
- zoomControl : true,
- mapTypeControl : true,
- scaleControl : true,
- streetViewControl : true,
- overviewMapControl : true
- };
- //---------------------------------------//
- // Create the map and elevator service: //
- //---------------------------------------//
- var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
- var elevator = new google.maps.ElevationService();
- //----------------------------------------------//
- // By default we add the double click listener: //
- //----------------------------------------------//
- addDoubleClickListener();
- //-------------------------------------------------------//
- // Make add and remove map double click event listeners: //
- //-------------------------------------------------------//
- function addDoubleClickListener()
- {
- if(mapDoubleClickListener != null)
- {
- log("addDoubleClickListener: mapDoubleClickListener was not null on mapId: " + mapId);
- mapDoubleClickListener = null;
- }
- try
- {
- mapDoubleClickListener = google.maps.event.addListener( map,
- 'dblclick',
- function(event)
- {
- mapDoubleClick(event.latLng);
- event.stop();
- });
- }
- catch(err)
- {
- log("addDoubleClickListener: " + err);
- }
- }
- function removeDoubleClickListener()
- {
- if(mapDoubleClickListener!=null)
- {
- try
- {
- google.maps.event.removeListener(mapDoubleClickListener);
- }
- catch(err)
- {
- log("removeDoubleClickListener: " + err);
- }
- mapDoubleClickListener = null;
- }
- else
- {
- log("removeDoubleClickListener: mapDoubleClickListener was already null on mapId: " + mapId);
- }
- }
- //------------------------------------------//
- // Create handlers for map double click: //
- //------------------------------------------//
- // mapDoubleClick:
- function mapDoubleClick(latLng)
- {
- log("mapDoubleClick: " + latLng);
- log("calling flash routine: " + "googleMapsDoubleClick_" + mapId);
- swfObject["floidMapDoubleClick_" + mapId](latLng.lat(), latLng.lng());
- }
- //********************************//
- //********************************//
- // Calls from Flash to the map: //
- //********************************//
- //********************************//
- ////////////////////////////////////////////////
- // Function creators for flash calls to map: //
- ////////////////////////////////////////////////
- // -------------------------------
- // floidGoogleMapsCreateNewMarker:
- // -------------------------------
- function makeFloidGoogleMapsCreateNewMarkerFunction()
- {
- return function(lat, lng, markerId)
- {
- log("floidGoogleMapsCreateNewMarker: " + lat + " " + lng + " " + markerId);
- createNewMarker(new google.maps.LatLng(lat, lng, true), markerId);
- }
- }
- // -----------------------------
- // floidGoogleMapsCreateNewLine:
- // -----------------------------
- function makeFloidGoogleMapsCreateNewLineFunction()
- {
- return function(lineId, startLat, startLng, endLat, endLng, a, r, g, b, w)
- {
- log("floidGoogleMapsCreateNewLine: " + startLat + " " + startLng + " " + endLat + " " + endLng + " " + a + " " + r + " " + g + " " + b);
- createNewLine(lineId, startLat, startLng, endLat, endLng, a, r, g, b, w);
- }
- }
- // --------------------------------------
- // floidGoogleMapsAddDoubleClickListener:
- // --------------------------------------
- function makeFloidGoogleMapsAddDoubleClickListenerFunction()
- {
- return function(mapId)
- {
- log("floidGoogleMapsAddDoubleClickListener: " + mapId);
- addDoubleClickListener();
- }
- }
- // -----------------------------------------
- // floidGoogleMapsRemoveDoubleClickListener:
- // -----------------------------------------
- function makeFloidGoogleMapsRemoveDoubleClickListenerFunction()
- {
- return function(mapId)
- {
- log("floidGoogleMapsRemoveDoubleClickListener: " + mapId);
- removeDoubleClickListener();
- }
- }
- // ----------------------------------
- // floidGoogleMapsGetPathMaxElevation:
- // ----------------------------------
- function makeFloidGoogleMapsGetPathMaxElevationFunction()
- {
- return function(lineId, startLat, startLng, endLat, endLng, maxMarkers, requestedPathDistanceInMeters, errorElevation, warningElevation, actualMarkers, actualPathDistanceInMeters)
- {
- log("floidGoogleMapsGetPathMaxElevation: " + startLat + " " + startLng + " " + endLat + " " + endLng + " " + maxMarkers + " " + requestedPathDistanceInMeters + " " + errorElevation + " " + warningElevation + " " + actualMarkers + " " + actualPathDistanceInMeters);
- getPathMaxElevation(lineId, startLat, startLng, endLat, endLng, maxMarkers, requestedPathDistanceInMeters, errorElevation, warningElevation, actualMarkers, actualPathDistanceInMeters);
- }
- }
- //////////////////////////////////////////////////////////////////////////////
- // Make a set of function creation routines for the flash calls to the DOM: //
- //////////////////////////////////////////////////////////////////////////////
- // -------------------------------
- // floidGoogleMapsCreateNewMarker:
- // -------------------------------
- var floidGoogleMapsCreateNewMarkerFunctionName = "floidMapCreateNewMarker_" + mapId;
- this.parent[floidGoogleMapsCreateNewMarkerFunctionName] = makeFloidGoogleMapsCreateNewMarkerFunction();
- // -----------------------------
- // floidGoogleMapsCreateNewLine:
- // -----------------------------
- var floidGoogleMapsCreateNewLineFunctionName = "floidMapCreateNewLine_" + mapId;
- this.parent[floidGoogleMapsCreateNewLineFunctionName] = makeFloidGoogleMapsCreateNewLineFunction();
- // --------------------------------------
- // floidGoogleMapsAddDoubleClickListener:
- // --------------------------------------
- var floidGoogleMapsAddDoubleClickListenerFunctionName = "floidMapAddDoubleClickListener_" + mapId;
- this.parent[floidGoogleMapsAddDoubleClickListenerFunctionName] = makeFloidGoogleMapsAddDoubleClickListenerFunction();
- // -----------------------------------------
- // floidGoogleMapsRemoveDoubleClickListener:
- // -----------------------------------------
- var floidGoogleMapsRemoveDoubleClickListenerFunctionName = "floidMapRemoveDoubleClickListener_" + mapId;
- this.parent[floidGoogleMapsRemoveDoubleClickListenerFunctionName] = makeFloidGoogleMapsRemoveDoubleClickListenerFunction();
- // --------------------------------
- // floidGoogleMapsGetPathMaxElevation:
- // --------------------------------
- var floidGoogleMapsGetPathMaxElevationFunctionName = "floidMapGetPathMaxElevation_" + mapId;
- this.parent[floidGoogleMapsGetPathMaxElevationFunctionName] = makeFloidGoogleMapsGetPathMaxElevationFunction();
- /////////////////////////////
- // Map routines: //
- /////////////////////////////
- // ----- MARKERS -----
- // ----------------
- // createNewMarker:
- // ----------------
- function createNewMarker(latLng, markerId)
- {
- log("Making new marker [" + mapId + "] ("+latLng.lat + ", " + latLng.lng + ")");
- var marker = new google.maps.Marker({
- position: latLng,
- map: map,
- radius: 12,
- hasShadow: true,
- clickable: true,
- draggable: true,
- gravity: 0.5,
- distanceScaling: false
- });
- ////////////////////////////////////////////////
- // Marker listeners that call back to flash: //
- ////////////////////////////////////////////////
- // ------------------------------------------------------
- // OK, here we want to add the listeners for this marker:
- // ------------------------------------------------------
- google.maps.event.addListener( marker,
- 'dblclick',
- function()
- {
- swfObject["floidMapMarkerDoubleClick_" + mapId+"_"+markerId](markerId);
- });
- google.maps.event.addListener( marker,
- 'dragstart',
- function(event)
- {
- swfObject["floidMapMarkerDragStart_" + mapId+"_"+markerId](markerId, event.latLng.lat(), event.latLng.lng());
- });
- google.maps.event.addListener( marker,
- 'dragend',
- function(event)
- {
- swfObject["floidMapMarkerDragEnd_" + mapId+"_"+markerId](markerId, event.latLng.lat(), event.latLng.lng());
- });
- google.maps.event.addListener( marker,
- 'drag',
- function(event)
- {
- swfObject["floidMapMarkerDrag_" + mapId+"_"+markerId](markerId, event.latLng.lat(), event.latLng.lng());
- });
- //***********************************//
- //***********************************//
- // Calls from Flash to the marker: //
- //***********************************//
- //***********************************//
- ///////////////////////////////////////////////////
- // Function creators for flash calls to marker: //
- ///////////////////////////////////////////////////
- // -----------------------------------------------------
- // floidGoogleMapsMarkerSetClickable: [function creator]
- // -----------------------------------------------------
- function makeFloidGoogleMapsMarkerSetClickableFunction(marker, markerId)
- {
- return function(clickable)
- {
- log("floidGoogleMapsMarkerSettingClickable: " + markerId + " to " + clickable);
- marker.setClickable(clickable);
- }
- }
- // -----------------------------------------------------
- // floidGoogleMapsMarkerSetDraggable: [function creator]
- // -----------------------------------------------------
- function makeFloidGoogleMapsMarkerSetDraggableFunction(marker, markerId)
- {
- return function(draggable)
- {
- log("floidGoogleMapsMarkerSettingDraggable: " + markerId + " to " + draggable);
- marker.setDraggable(draggable);
- }
- }
- // -----------------------------------------------------
- // floidGoogleMapsMarkerSetIconString: [function creator]
- // -----------------------------------------------------
- function makeFloidGoogleMapsMarkerSetIconStringFunction(marker, markerId)
- {
- return function(iconString)
- {
- log("floidGoogleMapsMarkerSetIconString: " + markerId);
- log("marker icon string: " + iconString);
- var markerIcon = {url: 'data:image/png;base64,'+iconString,
- size: new google.maps.Size(100, 38),
- origin: new google.maps.Point(0, 0),
- anchor: new google.maps.Point(0, 38)};
- log("floidGoogleMapsMarkerSetIconString: " + markerId + " - markerIconMade");
- marker.setIcon(markerIcon);
- log("floidGoogleMapsMarkerSetIconString: " + markerId + " - markerIconSet");
- }
- }
- // ------------------------------------------------------------
- // floidGoogleMapsMarkerSetIconShadowString: [function creator]
- // ------------------------------------------------------------
- function makeFloidGoogleMapsMarkerSetIconShadowStringFunction(marker, markerId)
- {
- return function(iconShadowString)
- {
- log("floidGoogleMapsMarkerSetIconShadowString: " + markerId);
- log("marker shadow string: " + iconShadowString);
- var markerIconShadow = {url: 'data:image/png;base64,'+iconShadowString,
- size: new google.maps.Size(132, 20),
- origin: new google.maps.Point(0, 0),
- anchor: new google.maps.Point(0, 16)};
- log("floidGoogleMapsMarkerSetIconString: " + markerId + " - markerIconShadowMade");
- marker.setShadow(markerIconShadow);
- log("floidGoogleMapsMarkerSetIconString: " + markerId + " - markerIconShadowSet");
- }
- }
- // ---------------------------------------------------------
- // floidGoogleMapsMarkerRequestElevation: [function creator]
- // ---------------------------------------------------------
- function makeFloidGoogleMapsMarkerRequestElevationFunction(marker, markerId)
- {
- return function(markerId, lat, lng)
- {
- log("floidGoogleMapsMarkerRequestElevation: " + markerId + " - Requesting Elevation for: " + lat + " " + lng);
- // Locations parameter:
- var latLng = new google.maps.LatLng(lat, lng);
- var locations = [];
- locations.push(latLng);
- // Create a LocationElevationRequest object using the array's one value
- var positionalRequest = {
- 'locations': locations
- };
- // Initiate the location request
- elevator.getElevationForLocations(positionalRequest,
- function(results, status)
- {
- log("got elevation result");
- if (status == google.maps.ElevationStatus.OK)
- {
- log("got ok elevation result");
- // Retrieve the first result
- if (results[0])
- {
- log("results[0] was good: " + results[0].elevation);
- // call back into flash :)
- swfObject["floidMapMarkerElevationResult_" + mapId+"_"+markerId](markerId, results[0].elevation);
- }
- else
- {
- log("No results found");
- }
- }
- else
- {
- log("Elevation service failed due to: " + status);
- }
- });
- log("floidGoogleMapsMarkerRequestElevation: " + markerId + " - Elevation Requested");
- }
- }
- // -----------------------------------------------
- // floidGoogleMapsRemoveMarker: [function creator]
- // -----------------------------------------------
- function makeFloidGoogleMapsRemoveMarkerFunction(marker, markerId)
- {
- return function()
- {
- log("Removing marker.");
- log("floidGoogleMapsRemoveMarker: " + mapId + " " + markerId);
- try
- {
- // Remove the marker:
- marker.setMap(null);
- marker = null;
- // Remove the globally bound functions:
- delete this.parent[floidGoogleMapsMarkerSetClickableFunctionName];
- delete this.parent[floidGoogleMapsMarkerSetDraggableFunctionName];
- delete this.parent[floidGoogleMapsMarkerRequestElevationFunctionName];
- delete this.parent[floidGoogleMapsMarkerSetIconStringFunctionName];
- delete this.parent[floidGoogleMapsMarkerSetIconShadowStringFunctionName];
- // Don't forget me!
- delete this.parent[floidGoogleMapsRemoveMarkerFunctionName];
- }
- catch(err)
- {
- log("floidGoogleMapsRemoveMarker: error " + err);
- log("floidGoogleMapsRemoveMarker: error " + mapId + " " + markerId + " " + err);
- }
- log("Done removing marker.");
- }
- }
- ///////////////////////////////////////////////////////
- // Add the flash calls for this marker to the DOM: //
- ///////////////////////////////////////////////////////
- // -----------------------------------------------------
- // floidGoogleMapsMarkerSetClickable:
- // -----------------------------------------------------
- var floidGoogleMapsMarkerSetClickableFunctionName = "floidMapMarkerSetClickable_" + mapId + "_" + markerId;
- this.parent[floidGoogleMapsMarkerSetClickableFunctionName] = makeFloidGoogleMapsMarkerSetClickableFunction(marker, markerId);
- // -----------------------------------------------------
- // floidGoogleMapsMarkerSetDraggable:
- // -----------------------------------------------------
- var floidGoogleMapsMarkerSetDraggableFunctionName = "floidMapMarkerSetDraggable_" + mapId + "_" + markerId;
- this.parent[floidGoogleMapsMarkerSetDraggableFunctionName] = makeFloidGoogleMapsMarkerSetDraggableFunction(marker, markerId);
- // -----------------------------------------------------
- // floidGoogleMapsMarkerRequestElevation:
- // -----------------------------------------------------
- var floidGoogleMapsMarkerRequestElevationFunctionName = "floidMapMarkerRequestElevation_" + mapId + "_" + markerId;
- this.parent[floidGoogleMapsMarkerRequestElevationFunctionName] = makeFloidGoogleMapsMarkerRequestElevationFunction(marker, markerId);
- // -----------------------------------------------------
- // floidGoogleMapsMarkerSetIconString:
- // -----------------------------------------------------
- var floidGoogleMapsMarkerSetIconStringFunctionName = "floidMapMarkerSetIconString_" + mapId + "_" + markerId;
- this.parent[floidGoogleMapsMarkerSetIconStringFunctionName] = makeFloidGoogleMapsMarkerSetIconStringFunction(marker, markerId);
- // -----------------------------------------------------
- // floidGoogleMapsMarkerSetIconShadowString:
- // -----------------------------------------------------
- var floidGoogleMapsMarkerSetIconShadowStringFunctionName = "floidMapMarkerSetIconShadowString_" + mapId + "_" + markerId;
- this.parent[floidGoogleMapsMarkerSetIconShadowStringFunctionName] = makeFloidGoogleMapsMarkerSetIconShadowStringFunction(marker, markerId);
- // ----------------------------
- // floidGoogleMapsRemoveMarker:
- // ----------------------------
- var floidGoogleMapsRemoveMarkerFunctionName = "floidMapRemoveMarker_" + mapId + "_" + markerId;
- this.parent[floidGoogleMapsRemoveMarkerFunctionName] = makeFloidGoogleMapsRemoveMarkerFunction(marker, markerId);
- }
- // ----- LINES -----
- // --------------
- // createNewLine:
- // --------------
- function createNewLine(lineId, startLat, startLng, endLat, endLng, a, r, g, b, w)
- {
- var lineCoordinates = [
- new google.maps.LatLng(startLat, startLng),
- new google.maps.LatLng(endLat, endLng)
- ];
- var strokeOpacity = a / 255.0;
- var strokeColor = rgbToHex(r, g, b);
- var line = new google.maps.Polyline( {
- path: lineCoordinates,
- strokeOpacity: strokeOpacity,
- strokeColor: strokeColor,
- clickable: false,
- editable: false,
- geodesic: true,
- map: map
- }
- );
- //////////////////////////////////////////////
- // Line listeners that call back to flash: //
- //////////////////////////////////////////////
- // ----------------------------------------------------
- // OK, here we want to add the listeners for this line:
- // ----------------------------------------------------
- // Default is no listeners for this line:
- //***********************************//
- //***********************************//
- // Calls from Flash to the line: //
- //***********************************//
- //***********************************//
- /////////////////////////////////////////////////
- // Function creators for flash calls to line: //
- /////////////////////////////////////////////////
- // -----------------------------------------------
- // floidGoogleMapsRemoveLine: [function creator]
- // -----------------------------------------------
- function makeFloidGoogleMapsRemoveLineFunction(line, lineId)
- {
- return function()
- {
- log("Removing line.");
- try
- {
- // Remove the line:
- line.setMap(null);
- // Remove the globally bound functions:
- // - empty
- // Don't forget me!
- delete this.parent[floidGoogleMapsRemoveLineFunctionName];
- }
- catch(err)
- {
- log("floidGoogleMapsRemoveMarker: error " + err);
- }
- log("Done removing line.");
- }
- }
- // -----------------------------------------------
- // floidGoogleMapsSetLineColor: [function creator]
- // -----------------------------------------------
- function makeFloidGoogleMapsSetLineColorFunction(line, lineId)
- {
- return function(a, r, g, b, w)
- {
- var newOpacity = a/255.0;
- var newColor = rgbToHex(r, g, b);
- log("Changing line color to: " + newColor);
- try
- {
- line.setOptions({strokeColor: newColor, strokeOpacity: newOpacity, strokeWeight: w});
- }
- catch(err)
- {
- log("floidGoogleMapsSetLineColor: error " + err);
- }
- log("Done setting line color.");
- }
- }
- // -------------------------------------------
- // floidGoogleMapsMoveLine: [function creator]
- // -------------------------------------------
- function makeFloidGoogleMapsMoveLineFunction(line, lineId)
- {
- return function(startLat, startLng, endLat, endLng)
- {
- log("Moving line [" + lineId + "] to: " + startLat + " " + startLng + " -> " + endLat + " " + endLng);
- try
- {
- var lineCoordinates = [
- new google.maps.LatLng(startLat, startLng),
- new google.maps.LatLng(endLat, endLng)
- ];
- line.setPath(lineCoordinates);
- }
- catch(err)
- {
- log("floidGoogleMapsMoveLine: error " + err);
- }
- log("Done moving line.");
- }
- }
- /////////////////////////////////////////////////////
- // Add the flash calls for this line to the DOM: //
- /////////////////////////////////////////////////////
- // -----------------------------------------------------
- // floidGoogleMapsRemoveLineFunction:
- // -----------------------------------------------------
- var floidGoogleMapsRemoveLineFunctionName = "floidMapRemoveLine_" + mapId + "_" + lineId;
- this.parent[floidGoogleMapsRemoveLineFunctionName] = makeFloidGoogleMapsRemoveLineFunction(line, lineId);
- // -----------------------------------------------------
- // floidGoogleMapsSetLineColorFunction:
- // -----------------------------------------------------
- var floidGoogleMapsSetLineColorFunctionName = "floidMapSetLineColor_" + mapId + "_" + lineId;
- this.parent[floidGoogleMapsSetLineColorFunctionName] = makeFloidGoogleMapsSetLineColorFunction(line, lineId);
- // -----------------------------------------------------
- // floidGoogleMapsMoveLineFunction:
- // -----------------------------------------------------
- var floidGoogleMapsMoveLineFunctionName = "floidMapMoveLine__" + mapId + "_" + lineId;
- this.parent[floidGoogleMapsMoveLineFunctionName] = makeFloidGoogleMapsMoveLineFunction(line, lineId);
- }
- // ------ Paths -------
- // --------------------
- // getPathMaxElevation:
- // --------------------
- function getPathMaxElevation(lineId, startLat, startLng, endLat, endLng, maxSamples, requestedPathDistanceInMeters, errorElevation, warningElevation, actualSamples, actualPathDistanceInMeters)
- {
- var sampleLocations = calculateSamples(startLat, startLng, endLat, endLng, actualSamples);
- // OK, make an array of sample points to test:
- var samplePositions = [];
- for(var i=0; i<sampleLocations.length/2; ++i)
- {
- var sampleLatLng = new google.maps.LatLng(sampleLocations[i*2], sampleLocations[i*2+1]);
- samplePositions.push(sampleLatLng);
- }
- // Then make a request to google maps:
- var positionalRequest = {
- 'locations': samplePositions
- };
- // Send the location request:
- elevator.getElevationForLocations(positionalRequest,
- function(results, status)
- {
- // Note this is in meters:
- log("got path max elevation results");
- var maxElevation = -100000; // Min value
- var errorFlag = true;
- if (status == google.maps.ElevationStatus.OK)
- {
- log("got ok path max elevation results");
- // Retrieve the first result
- if (results[0])
- {
- log("getPathMaxElevation results[0] was good: " + results[0].elevation);
- maxElevation = results[0].elevation;
- for(var e=0; e<results.length; ++e)
- {
- log("elevation: " + results[e].elevation);
- if(results[e].elevation>maxElevation)
- {
- maxElevation = results[e].elevation
- }
- }
- errorFlag = false;
- }
- else
- {
- log("getPathMaxElevation: No results found");
- }
- }
- else
- {
- log("getPathMaxElevation: Service failed due to: " + status);
- }
- // call back into flash :)
- swfObject["floidMapPathMaxElevationCallBack_" + mapId](lineId, startLat, startLng, endLat, endLng, maxSamples, requestedPathDistanceInMeters, errorElevation, warningElevation, maxElevation, actualSamples, actualPathDistanceInMeters, errorFlag);
- });
- log("getMaxPathElevation: " + lineId + " - Elevation Requested");
- }
- // OK, the map is fully initialized - call back into flash:
- swfObject["floidMapInitialized_" + mapId]();
- }
- function finalize()
- {
- // Remove global map functions - TODO: remove remaining marker & line functions
- try
- {
- delete this.parent[floidGoogleMapsCreateNewMarkerFunctionName];
- delete this.parent[floidGoogleMapsCreateNewLineFunctionName];
- delete this.parent[floidGoogleMapsAddDoubleClickListenerFunctionName];
- delete this.parent[floidGoogleMapsRemoveDoubleClickListenerFunctionName];
- delete this.parent[floidGoogleMapsGetPathMaxElevationFunctionName];
- }
- catch(err)
- {
- log("Removing map methods error: " + err);
- }
- if(swfObject != null)
- {
- // call back into flash :)
- swfObject["floidMapFrameUnloaded_" + mapId]();
- }
- }
- </script>
- </head>
- <body onload="initialize()" onunload="finalize()">
- <div id="map_canvas" style="width:100%; height:100%"></div>
- </body>
- </html>
- FloidMapQuestMapFrame.html - MapQuest html component:
- <!DOCTYPE html>
- <html>
- <head>
- <!-- faigelabs - test code for Floid maps - cfaigle - free -->
- <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
- <style type="text/css">
- html { height: 100% }
- body { height: 100%; margin: 0; padding: 0 }
- #map_canvas { height: 100% }
- </style>
- <script type ="text/javascript"
- src ="FloidMapUtilities.js">
- </script>
- <script type ="text/javascript"
- src ="jquery-1.9.1.min.js">
- </script>
- <script type ="text/javascript"
- src ="http://www.mapquestapi.com/sdk/js/v7.0.s/mqa.toolkit.js?key=YOUR_API_KEY">
- </script>
- <script type="text/javascript">
- ///////////////////////////////////////////
- ///////////////////////////////////////////
- // Main Initialize - Magic starts here: //
- ///////////////////////////////////////////
- ///////////////////////////////////////////
- function initialize()
- {
- // Could not get the listener to remove so we will just keep a flag:
- var doubleClickable = false;
- function mapDoubleClickListener(event)
- {
- if(doubleClickable)
- {
- mapDoubleClick(event.ll);
- }
- };
- //----------------------------------//
- // Create the default map options: //
- //----------------------------------//
- var mapOptions = {
- elt : document.getElementById('map_canvas'),
- zoom : 12,
- latLng : { lat:latStart, lng: lngStart },
- mtype : 'map',
- bestFitMargin : 0,
- zoomOnDoubleClick : false
- };
- //---------------------------------------//
- // Create the map and elevator service: //
- //---------------------------------------//
- var map = new MQA.TileMap(mapOptions);
- MQA.withModule('largezoom',
- 'viewoptions',
- 'mousewheel',
- 'traffictoggle',
- 'viewoptions',
- 'geolocationcontrol',
- 'insetmapcontrol',
- function()
- {
- map.addControl(new MQA.ViewOptions());
- map.addControl(new MQA.LargeZoom(), new MQA.MapCornerPlacement(MQA.MapCorner.TOP_LEFT, new MQA.Size(5,5)));
- map.enableMouseWheelZoom();
- });
- //----------------------------------------------//
- // By default we add the double click listener: //
- //----------------------------------------------//
- // In mapQuest we add it one time and then turn it on:
- MQA.EventManager.addListener(map, 'doubleclick', mapDoubleClickListener);
- addDoubleClickListener();
- //-------------------------------------------------------//
- // Make add and remove map double click event listeners: //
- //-------------------------------------------------------//
- function addDoubleClickListener()
- {
- log("addDoubleClickListener: " + mapId);
- doubleClickable = true;
- }
- function removeDoubleClickListener()
- {
- log("removeDoubleClickListener: " + mapId);
- doubleClickable = false;
- }
- //------------------------------------------//
- // Create handlers for map double click: //
- //------------------------------------------//
- // mapDoubleClick:
- function mapDoubleClick(latLng)
- {
- log("mapDoubleClick: " + latLng);
- log("calling flash routine: " + "floidMapDoubleClick_" + mapId);
- swfObject["floidMapDoubleClick_" + mapId](latLng.lat, latLng.lng);
- }
- //********************************//
- //********************************//
- // Calls from Flash to the map: //
- //********************************//
- //********************************//
- ////////////////////////////////////////////////
- // Function creators for flash calls to map: //
- ////////////////////////////////////////////////
- // ---------------------------------
- // floidMapQuestMapsCreateNewMarker:
- // ---------------------------------
- function makeFloidMapQuestMapsCreateNewMarkerFunction()
- {
- return function(lat, lng, markerId)
- {
- log("floidMapQuestMapsCreateNewMarker: " + lat + " " + lng + " " + markerId);
- createNewMarker(lat, lng, markerId);
- }
- }
- // -------------------------------
- // floidMapQuestMapsCreateNewLine:
- // -------------------------------
- function makeFloidMapQuestMapsCreateNewLineFunction()
- {
- return function(lineId, startLat, startLng, endLat, endLng, a, r, g, b, w)
- {
- log("floidMapQuestMapsCreateNewLine: " + startLat + " " + startLng + " " + endLat + " " + endLng + " " + a + " " + r + " " + g + " " + b);
- createNewLine(lineId, startLat, startLng, endLat, endLng, a, r, g, b, w);
- }
- }
- // ----------------------------------------
- // floidMapQuestMapsAddDoubleClickListener:
- // ----------------------------------------
- function makeFloidMapQuestMapsAddDoubleClickListenerFunction()
- {
- return function(mapId)
- {
- log("floidMapQuestMapsAddDoubleClickListener: " + mapId);
- addDoubleClickListener();
- }
- }
- // -------------------------------------------
- // floidMapQuestMapsRemoveDoubleClickListener:
- // -------------------------------------------
- function makeFloidMapQuestMapsRemoveDoubleClickListenerFunction()
- {
- return function(mapId)
- {
- log("floidMapQuestMapsRemoveDoubleClickListener: " + mapId);
- removeDoubleClickListener();
- }
- }
- // ----------------------------------
- // floidMapQuestMapsGetPathMaxElevation:
- // ----------------------------------
- function makeFloidMapQuestMapsGetPathMaxElevationFunction()
- {
- return function(lineId, startLat, startLng, endLat, endLng, maxMarkers, requestedPathDistanceInMeters, errorElevation, warningElevation, actualMarkers, actualPathDistanceInMeters)
- {
- log("floidMapQuestMapsGetPathMaxElevation: " + startLat + " " + startLng + " " + endLat + " " + endLng + " " + maxMarkers + " " + requestedPathDistanceInMeters + " " + errorElevation + " " + warningElevation + " " + actualMarkers + " " + actualPathDistanceInMeters);
- getPathMaxElevation(lineId, startLat, startLng, endLat, endLng, maxMarkers, requestedPathDistanceInMeters, errorElevation, warningElevation, actualMarkers, actualPathDistanceInMeters);
- }
- }
- //////////////////////////////////////////////////////////////////////////////
- // Make a set of function creation routines for the flash calls to the DOM: //
- //////////////////////////////////////////////////////////////////////////////
- // ----------------------------------
- // floidMapQuestMapsCreateNewMarker:
- // ----------------------------------
- var floidMapQuestMapsCreateNewMarkerFunctionName = "floidMapCreateNewMarker_" + mapId;
- this.parent[floidMapQuestMapsCreateNewMarkerFunctionName] = makeFloidMapQuestMapsCreateNewMarkerFunction();
- // --------------------------------
- // floidMapQuestMapsCreateNewLine:
- // --------------------------------
- var floidMapQuestMapsCreateNewLineFunctionName = "floidMapCreateNewLine_" + mapId;
- this.parent[floidMapQuestMapsCreateNewLineFunctionName] = makeFloidMapQuestMapsCreateNewLineFunction();
- // -----------------------------------------
- // floidMapQuestMapsAddDoubleClickListener:
- // -----------------------------------------
- var floidMapQuestMapsAddDoubleClickListenerFunctionName = "floidMapAddDoubleClickListener_" + mapId;
- this.parent[floidMapQuestMapsAddDoubleClickListenerFunctionName] = makeFloidMapQuestMapsAddDoubleClickListenerFunction();
- // --------------------------------------------
- // floidMapQuestMapsRemoveDoubleClickListener:
- // --------------------------------------------
- var floidMapQuestMapsRemoveDoubleClickListenerFunctionName = "floidMapRemoveDoubleClickListener_" + mapId;
- this.parent[floidMapQuestMapsRemoveDoubleClickListenerFunctionName] = makeFloidMapQuestMapsRemoveDoubleClickListenerFunction();
- // --------------------------------
- // floidMapQuestMapsGetPathMaxElevation:
- // --------------------------------
- var floidMapQuestMapsGetPathMaxElevationFunctionName = "floidMapGetPathMaxElevation_" + mapId;
- this.parent[floidMapQuestMapsGetPathMaxElevationFunctionName] = makeFloidMapQuestMapsGetPathMaxElevationFunction();
- /////////////////////////////
- // Map routines: //
- /////////////////////////////
- // We have to store the marker listeners & clickable state:
- var markerDoubleClickListener = null;
- var markerDragStartListener = null;
- var markerDragEndListener = null;
- var markerDragListener = null;
- var clickable = true;
- // ----- MARKERS -----
- // ----------------
- // createNewMarker:
- // ----------------
- function createNewMarker(lat, lng, markerId)
- {
- var marker = new MQA.Poi({lat: lat, lng: lng});
- log("Making new marker [" + mapId + "] ("+lat + ", " + lng + ")");
- marker.draggable = true;
- map.addShape(marker);
- ////////////////////////////////////////////////
- // Marker listeners that call back to flash: //
- ////////////////////////////////////////////////
- // ------------------------------------------------------
- // OK, here we want to add the listeners for this marker:
- // ------------------------------------------------------
- markerDoubleClickListener = function()
- {
- if(clickable)
- {
- swfObject["floidMapMarkerDoubleClick_" + mapId+"_"+markerId](markerId);
- }
- };
- MQA.EventManager.addListener(marker, 'dblclick', markerDoubleClickListener); // NOTE: dblclick v doubleclick - THIS IS DIFFERENT BETWEEN MAPS AND POI'S
- markerDragStartListener = function(event)
- {
- swfObject["floidMapMarkerDragStart_" + mapId+"_"+markerId](markerId, event.srcObject.latLng.lat, event.srcObject.latLng.lng);
- };
- MQA.EventManager.addListener(marker, 'dragstart', markerDragStartListener);
- markerDragEndListener = function(event)
- {
- swfObject["floidMapMarkerDragEnd_" + mapId+"_"+markerId](markerId, event.srcObject.latLng.lat, event.srcObject.latLng.lng);
- };
- MQA.EventManager.addListener(marker, 'dragend', markerDragEndListener);
- markerDragListener = function(event)
- {
- swfObject["floidMapMarkerDrag_" + mapId+"_"+markerId](markerId, event.srcObject.latLng.lat, event.srcObject.latLng.lng);
- };
- MQA.EventManager.addListener(marker, 'drag', markerDragListener);
- //***********************************//
- //***********************************//
- // Calls from Flash to the marker: //
- //***********************************//
- //***********************************//
- ///////////////////////////////////////////////////
- // Function creators for flash calls to marker: //
- ///////////////////////////////////////////////////
- // -------------------------------------------------------
- // floidMapQuestMapsMarkerSetClickable: [function creator]
- // -------------------------------------------------------
- function makeFloidMapQuestMapsMarkerSetClickableFunction(marker, markerId)
- {
- return function(isClickable)
- {
- clickable = isClickable;
- }
- }
- // -------------------------------------------------------
- // floidMapQuestMapsMarkerSetDraggable: [function creator]
- // -------------------------------------------------------
- function makeFloidMapQuestMapsMarkerSetDraggableFunction(marker, markerId)
- {
- return function(draggable)
- {
- marker.setDraggable(draggable);
- }
- }
- // --------------------------------------------------------
- // floidMapQuestMapsMarkerSetIconString: [function creator]
- // --------------------------------------------------------
- function makeFloidMapQuestMapsMarkerSetIconStringFunction(marker, markerId)
- {
- return function(iconString)
- {
- log("floidMapQuestMapsMarkerSetIconString: " + markerId);
- log("marker icon string: " + iconString);
- marker.setIcon(new MQA.Icon('data:image/png;base64,'+iconString, 100, 38));
- marker.setIconOffset(new MQA.Point(0,-38));
- log("floidMapQuestMapsMarkerSetIconString: " + markerId + " - markerIconSet");
- }
- }
- // --------------------------------------------------------------
- // floidMapQuestMapsMarkerSetIconShadowString: [function creator]
- // --------------------------------------------------------------
- function makeFloidMapQuestMapsMarkerSetIconShadowStringFunction(marker, markerId)
- {
- return function(iconShadowString)
- {
- log("floidMapQuestMapsMarkerSetIconShadowString: " + markerId);
- log("marker shadow string: " + iconShadowString);
- log("floidMapQuestMapsMarkerSetIconString: " + markerId);
- log("marker icon shadow string: " + iconShadowString);
- marker.setShadow(new MQA.Icon('data:image/png;base64,'+iconShadowString, 132, 20));
- log("floidMapQuestMapsMarkerSetIconString: " + markerId + " - markerIconSet");
- marker.setShadowOffset(new MQA.Point(0,-20));
- log("floidMapQuestMapsMarkerSetIconString: " + markerId + " - markerIconShadowSet");
- }
- }
- // -----------------------------------------------------------
- // floidMapQuestMapsMarkerRequestElevation: [function creator]
- // -----------------------------------------------------------
- function makeFloidMapQuestMapsMarkerRequestElevationFunction(marker, markerId)
- {
- return function(markerId, lat, lng)
- {
- // Build the url string:
- // OK, make an array of sample points to test, but with only one point:
- var sampleLocations = [lat, lng];
- // Make elevation request object:
- var elevationRequestObject = {
- 'unit' : 'm',
- 'latLngCollection': sampleLocations
- };
- // Convert to JSON:
- var elevationRequestJSONString = JSON.stringify(elevationRequestObject);
- log("Elevation JSON String: " + elevationRequestJSONString);
- var mapQuestElevationUrl = "http://open.mapquestapi.com/elevation/v1/profile?outFormat=json&json="+elevationRequestJSONString+"&callback=?";
- $.ajax({
- url: mapQuestElevationUrl,
- dataType: 'json',
- type: 'GET',
- contentType:'json',
- crossDomain:true,
- success: function(data) {
- log( data );
- if(data.elevationProfile.length >0)
- {
- var elevation = data.elevationProfile[0].height;
- errorFlag = false;
- log("Elevation of point: " + elevation);
- // call back into flash :)
- swfObject["floidMapMarkerElevationResult_" + mapId+"_"+markerId](markerId, elevation);
- log("elevation callback done:");
- }
- },
- error: function(data) {
- log( 'error occurred - ' + data );
- }
- });
- log("floidMapQuestMapsMarkerRequestElevation: " + markerId + " - Elevation Requested");
- }
- }
- // -------------------------------------------------
- // floidMapQuestMapsRemoveMarker: [function creator]
- // -------------------------------------------------
- function makeFloidMapQuestMapsRemoveMarkerFunction(marker, markerId)
- {
- return function()
- {
- log("Removing marker.");
- try
- {
- // Remove the marker:
- map.removeShape(marker);
- marker = null;
- // Remove the globally bound functions:
- delete this.parent[floidMapQuestMapsMarkerSetClickableFunctionName];
- delete this.parent[floidMapQuestMapsMarkerSetDraggableFunctionName];
- delete this.parent[floidMapQuestMapsMarkerRequestElevationFunctionName];
- delete this.parent[floidMapQuestMapsMarkerSetIconStringFunctionName];
- delete this.parent[floidMapQuestMapsMarkerSetIconShadowStringFunctionName];
- // Don't forget me!
- delete this.parent[floidMapQuestMapsRemoveMarkerFunctionName];
- }
- catch(err)
- {
- log("floidMapQuestMapsRemoveMarker: error " + err);
- }
- log("Done removing marker.");
- }
- }
- ///////////////////////////////////////////////////////
- // Add the flash calls for this marker to the DOM: //
- ///////////////////////////////////////////////////////
- // -----------------------------------------------------
- // floidMapQuestMapsMarkerSetClickable:
- // -----------------------------------------------------
- var floidMapQuestMapsMarkerSetClickableFunctionName = "floidMapMarkerSetClickable_" + mapId + "_" + markerId;
- this.parent[floidMapQuestMapsMarkerSetClickableFunctionName] = makeFloidMapQuestMapsMarkerSetClickableFunction(marker, markerId);
- // -----------------------------------------------------
- // floidMapQuestMapsMarkerSetDraggable:
- // -----------------------------------------------------
- var floidMapQuestMapsMarkerSetDraggableFunctionName = "floidMapMarkerSetDraggable_" + mapId + "_" + markerId;
- this.parent[floidMapQuestMapsMarkerSetDraggableFunctionName] = makeFloidMapQuestMapsMarkerSetDraggableFunction(marker, markerId);
- // -----------------------------------------------------
- // floidMapQuestMapsMarkerRequestElevation:
- // -----------------------------------------------------
- var floidMapQuestMapsMarkerRequestElevationFunctionName = "floidMapMarkerRequestElevation_" + mapId + "_" + markerId;
- this.parent[floidMapQuestMapsMarkerRequestElevationFunctionName] = makeFloidMapQuestMapsMarkerRequestElevationFunction(marker, markerId);
- // -----------------------------------------------------
- // floidMapQuestMapsMarkerSetIconString:
- // -----------------------------------------------------
- var floidMapQuestMapsMarkerSetIconStringFunctionName = "floidMapMarkerSetIconString_" + mapId + "_" + markerId;
- this.parent[floidMapQuestMapsMarkerSetIconStringFunctionName] = makeFloidMapQuestMapsMarkerSetIconStringFunction(marker, markerId);
- // -----------------------------------------------------
- // floidMapQuestMapsMarkerSetIconShadowString:
- // -----------------------------------------------------
- var floidMapQuestMapsMarkerSetIconShadowStringFunctionName = "floidMapMarkerSetIconShadowString_" + mapId + "_" + markerId;
- this.parent[floidMapQuestMapsMarkerSetIconShadowStringFunctionName] = makeFloidMapQuestMapsMarkerSetIconShadowStringFunction(marker, markerId);
- // ------------------------------
- // floidMapQuestMapsRemoveMarker:
- // ------------------------------
- var floidMapQuestMapsRemoveMarkerFunctionName = "floidMapRemoveMarker_" + mapId + "_" + markerId;
- this.parent[floidMapQuestMapsRemoveMarkerFunctionName] = makeFloidMapQuestMapsRemoveMarkerFunction(marker, markerId);
- }
- // ----- LINES -----
- // -----------------
- // createNewLine:
- // -----------------
- function createNewLine(lineId, startLat, startLng, endLat, endLng, a, r, g, b, w)
- {
- var newColorAlpha = a/255.0;
- var newColor = rgbToHex(r, g, b);
- var line = new MQA.LineOverlay();
- line.setShapePoints([startLat, startLng, endLat, endLng]);
- line.updateProperties( {color: newColor, colorAlpha: newColorAlpha, borderWidth: w});
- map.addShape(line);
- //////////////////////////////////////////////
- // Line listeners that call back to flash: //
- //////////////////////////////////////////////
- // ----------------------------------------------------
- // OK, here we want to add the listeners for this line:
- // ----------------------------------------------------
- // Default is no listeners for this line:
- //***********************************//
- //***********************************//
- // Calls from Flash to the line: //
- //***********************************//
- //***********************************//
- /////////////////////////////////////////////////
- // Function creators for flash calls to line: //
- /////////////////////////////////////////////////
- // -----------------------------------------------
- // floidMapQuestMapsRemoveLine: [function creator]
- // -----------------------------------------------
- function makeFloidMapQuestMapsRemoveLineFunction(line, lineId)
- {
- return function()
- {
- log("Removing line.");
- try
- {
- // Remove the line:
- map.removeShape(line);
- // Remove the globally bound functions:
- // - empty
- // Don't forget me!
- delete this.parent[floidMapQuestMapsRemoveLineFunctionName];
- }
- catch(err)
- {
- log("floidMapQuestMapsRemoveMarker: error " + err);
- }
- log("Done removing line.");
- }
- }
- // -----------------------------------------------
- // floidMapQuestMapsSetLineColor: [function creator]
- // -----------------------------------------------
- function makeFloidMapQuestMapsSetLineColorFunction(line, lineId)
- {
- return function(a, r, g, b, w)
- {
- var newColorAlpha = a/255.0;
- var newColor = rgbToHex(r, g, b);
- log("Changing line color: " + newColor);
- try
- {
- line.updateProperties( {color: newColor, colorAlpha: newColorAlpha, borderWidth: w});
- }
- catch(err)
- {
- log("floidMapQuestMapsSetLineColor: error " + err);
- }
- log("Done setting line color.");
- }
- }
- // ---------------------------------------------
- // floidMapQuestMapsMoveLine: [function creator]
- // ---------------------------------------------
- function makeFloidMapQuestMapsMoveLineFunction(line, lineId)
- {
- return function(startLat, startLng, endLat, endLng)
- {
- log("Moving line [" + lineId + "] to: " + startLat + " " + startLng + " -> " + endLat + " " + endLng);
- try
- {
- line.setShapePoints([startLat, startLng, endLat, endLng]);
- }
- catch(err)
- {
- log("floidMapQuestMapsMoveLine: error " + err);
- }
- log("Done moving line.");
- }
- }
- /////////////////////////////////////////////////////
- // Add the flash calls for this line to the DOM: //
- /////////////////////////////////////////////////////
- // -----------------------------------------------------
- // floidMapQuestMapsRemoveLineFunction:
- // -----------------------------------------------------
- var floidMapQuestMapsRemoveLineFunctionName = "floidMapRemoveLine_" + mapId + "_" + lineId;
- this.parent[floidMapQuestMapsRemoveLineFunctionName] = makeFloidMapQuestMapsRemoveLineFunction(line, lineId);
- // -----------------------------------------------------
- // floidMapQuestMapsSetLineColorFunction:
- // -----------------------------------------------------
- var floidMapQuestMapsSetLineColorFunctionName = "floidMapSetLineColor_" + mapId + "_" + lineId;
- this.parent[floidMapQuestMapsSetLineColorFunctionName] = makeFloidMapQuestMapsSetLineColorFunction(line, lineId);
- // -----------------------------------------------------
- // floidMapQuestMapsMoveLineFunction:
- // -----------------------------------------------------
- var floidMapQuestMapsMoveLineFunctionName = "floidMapMoveLine__" + mapId + "_" + lineId;
- this.parent[floidMapQuestMapsMoveLineFunctionName] = makeFloidMapQuestMapsMoveLineFunction(line, lineId);
- }
- // ------ Paths -------
- // --------------------
- // getPathMaxElevation:
- // --------------------
- function getPathMaxElevation(lineId, startLat, startLng, endLat, endLng, maxSamples, requestedPathDistanceInMeters, errorElevation, warningElevation, actualSamples, actualPathDistanceInMeters)
- {
- var sampleLocations = calculateSamples(startLat, startLng, endLat, endLng, actualSamples);
- // OK, make an array of sample points to test:
- var samplePositions = [];
- for(var i=0; i<sampleLocations.length/2; ++i)
- {
- samplePositions.push(sampleLocations[i*2], sampleLocations[i*2+1]);
- }
- // Make elevation request object:
- var elevationRequestObject = {
- 'unit' : 'm',
- 'latLngCollection': samplePositions
- };
- // Convert to JSON:
- var elevationRequestJSONString = JSON.stringify(elevationRequestObject);
- log("Elevation JSON String: " + elevationRequestJSONString);
- var mapQuestElevationUrl = "http://open.mapquestapi.com/elevation/v1/profile?outFormat=json&json="+elevationRequestJSONString+"&callback=?";
- $.ajax({
- url: mapQuestElevationUrl,
- dataType: 'json',
- type: 'GET',
- contentType:'json',
- crossDomain:true,
- success: function(data) {
- var errorFlag = true;
- var maxElevation = -100000;
- log( data );
- try
- {
- if(data.elevationProfile.length >0)
- {
- maxElevation = data.elevationProfile[0].height;
- errorFlag = false;
- for(var i=0; i<data.elevationProfile.length; ++i)
- {
- var sampleElevation = data.elevationProfile[i].height;
- if(sampleElevation > maxElevation) maxElevation = sampleElevation;
- }
- log("Max elevation of path: " + maxElevation);
- // call back into flash :)
- swfObject["floidMapPathMaxElevationCallBack_" + mapId](lineId, startLat, startLng, endLat, endLng, maxSamples, requestedPathDistanceInMeters, errorElevation, warningElevation, maxElevation, actualSamples, actualPathDistanceInMeters, errorFlag);
- log("elevation callback done:");
- }
- else
- {
- // call back into flash :)
- swfObject["floidMapPathMaxElevationCallBack_" + mapId](lineId, startLat, startLng, endLat, endLng, maxSamples, requestedPathDistanceInMeters, errorElevation, warningElevation, maxElevation, actualSamples, actualPathDistanceInMeters, errorFlag);
- }
- }
- catch(err)
- {
- swfObject["floidMapPathMaxElevationCallBack_" + mapId](lineId, startLat, startLng, endLat, endLng, maxSamples, requestedPathDistanceInMeters, errorElevation, warningElevation, maxElevation, actualSamples, actualPathDistanceInMeters, errorFlag);
- }
- },
- error: function(data) {
- log( 'getMaxPathElevation: error occurred - ' + data );
- var errorFlag = true;
- var maxElevation = -100000;
- // call back into flash :)
- swfObject["floidMapPathMaxElevationCallBack_" + mapId](lineId, startLat, startLng, endLat, endLng, maxSamples, requestedPathDistanceInMeters, errorElevation, warningElevation, maxElevation, actualSamples, actualPathDistanceInMeters, errorFlag);
- }
- });
- log("getPathMaxElevation: " + lineId + " sent");
- }
- // OK, the map is fully initialized - call back into flash:
- swfObject["floidMapInitialized_" + mapId]();
- }
- function finalize()
- {
- // Remove global map functions - TODO: remove remaining marker & line functions
- try
- {
- delete this.parent[floidMapQuestMapsCreateNewMarkerFunctionName];
- delete this.parent[floidMapQuestMapsCreateNewLineFunctionName];
- delete this.parent[floidMapQuestMapsAddDoubleClickListenerFunctionName];
- delete this.parent[floidMapQuestMapsRemoveDoubleClickListenerFunctionName];
- delete this.parent[floidMapQuestMapsGetPathMaxElevationFunctionName];
- }
- catch(err)
- {
- log("Removing map methods error: " + err);
- }
- if(swfObject != null)
- {
- // call back into flash :)
- swfObject["floidMapFrameUnloaded_" + mapId]();
- }
- }
- </script>
- </head>
- <body onload="initialize()" onunload="finalize()">
- <!-- Note that with height and width 100% below, the MapQuest map was not inset right -->
- <div id="map_canvas" style="width:400px; height:400px;"></div>
- </body>
- </html>
- FloidMapUtilities.js - Shared JavaScript utilities
- // FloidMapUtilities.js
- // faigelabs - test code for Floid maps - cfaigle - free
- //------------------------//
- // Parameters from URL: //
- //------------------------//
- var params = {};
- var mapId = 0;
- var latStart = 0;
- var lngStart = 0;
- //--------------------------------//
- // Console log utility function: //
- //--------------------------------//
- function log(message)
- {
- if(typeof console == "object")
- {
- console.log(message);
- }
- }
- //--------------------------------//
- // Storage for the flash object: //
- //--------------------------------//
- var swfObject = null;
- //-------------------------//
- // Parse the parameters: //
- //-------------------------//
- var prmstr = window.location.search.substr(1);
- var prmarr = prmstr.split ("&");
- for ( var i = 0; i < prmarr.length; i++)
- {
- var tmparr = prmarr[i].split("=");
- params[tmparr[0]] = tmparr[1];
- }
- //-------------------------//
- // Get the map id: //
- //-------------------------//
- if(params['mapId'] != null)
- {
- try
- {
- mapId=Number(params['mapId']);
- }
- catch(err)
- {
- mapId = 0;
- }
- }
- //-------------------------//
- // Get the lat start: //
- //-------------------------//
- if(params['latStart'] != null)
- {
- try
- {
- latStart=Number(params['latStart']);
- }
- catch(err)
- {
- latStart = 0;
- }
- }
- //-------------------------//
- // Get the lng start: //
- //-------------------------//
- if(params['lngStart'] != null)
- {
- try
- {
- lngStart=Number(params['lngStart']);
- }
- catch(err)
- {
- lngStart = 0;
- }
- }
- //-------------------------//
- // Get the flash object: //
- //-------------------------//
- log("obj name: " + params['obj']);
- swfObject = parent.document.getElementById(params['obj']);
- //---------------------//
- // Utility routines: //
- //---------------------//
- // Sample position calculation:
- function calculateSamples(startLat, startLng, endLat, endLng, actualSamples)
- {
- // OK, make an array of sample points to test:
- var sampleLocations = [];
- for(var i=0; i<actualSamples; ++i)
- {
- var factor = i / (actualSamples-1); // Note - again we are including both start and end points...
- log('factor: ' + factor);
- var sampleLat = startLat + (endLat-startLat) * factor;
- var sampleLng = startLng + (endLng-startLng) * factor;
- log('Sample: ' + sampleLat + " " + sampleLng);
- sampleLocations.push(sampleLat, sampleLng);
- }
- return sampleLocations;
- }
- // From: http://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb
- function componentToHex(c)
- {
- var hex = c.toString(16);
- return hex.length == 1 ? "0" + hex : hex;
- }
- function rgbToHex(r, g, b)
- {
- return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
- }
- function argbToHex(a, r, g, b)
- {
- return "#" + componentToHex(a) + componentToHex(r) + componentToHex(g) + componentToHex(b);
- }
- function rgbaToHex(r, g, b, a)
- {
- return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b) + componentToHex(a);
- }
- FloidPinAlt.as - Dynamic marker code:
- package com.faiglelabs.floid.testcode
- {
- // faigelabs - test code for Floid maps - cfaigle - free
- import flash.display.BitmapData;
- import flash.display.Shape;
- import flash.filters.BitmapFilterQuality;
- import flash.filters.GlowFilter;
- import flash.geom.ColorTransform;
- import flash.geom.Matrix;
- import flash.text.TextField;
- import flash.text.TextFormat;
- import flash.utils.ByteArray;
- import mx.graphics.codec.PNGEncoder;
- import com.foxarc.util.Base64;
- public class FloidPinAlt
- {
- public var pinName : String = "No Name";
- public function makeIcon():String
- {
- var iconShape : Shape = makeIconShape();
- var finalBitmapData : BitmapData = new BitmapData(100, 38, true, 0x0);
- var offsetMatrix : Matrix = new Matrix();
- offsetMatrix.ty = 0;
- offsetMatrix.tx = 0;
- finalBitmapData.draw(iconShape, offsetMatrix);
- var finalBitmapPNG : ByteArray = (new PNGEncoder()).encode(finalBitmapData);
- var encodedMarkerBitmap:String = Base64.encode(finalBitmapPNG);
- return encodedMarkerBitmap;
- }
- public function makeIconShadow():String
- {
- var iconShape : Shape = makeIconShape();
- var finalBitmapData : BitmapData = new BitmapData(132, 20, true, 0x0);
- var offsetMatrix : Matrix = new Matrix();
- offsetMatrix.ty = 0;
- offsetMatrix.tx = 20;
- offsetMatrix.c = -1;
- offsetMatrix.d = 0.5;
- var colorTransform : ColorTransform = new ColorTransform();
- colorTransform.alphaOffset = -160;
- colorTransform.redMultiplier = 0.9;
- colorTransform.greenMultiplier = 0.9;
- colorTransform.blueMultiplier = 0.9;
- colorTransform.redOffset = 0x10;
- colorTransform.greenOffset = 0x10;
- colorTransform.blueOffset = 0x10;
- finalBitmapData.draw(iconShape, offsetMatrix, colorTransform);
- var finalBitmapPNG : ByteArray = (new PNGEncoder()).encode(finalBitmapData);
- var encodedMarkerBitmap:String = Base64.encode(finalBitmapPNG);
- return encodedMarkerBitmap;
- }
- public function makeIconShape():Shape
- {
- var standardBackgroundColor:uint = 0xFF999999; // Gray
- var standardLineColor:uint = 0xFF000000; // Black
- var iconTextWidth:int = 120;
- var iconTextHeight:int = 20;
- var iconBarLocation:int = 18;
- var iconBarHeight:int = 6;
- var iconBarWidth:int = 100;
- var iconPointerStartX:int = 14;
- var iconPointerEndX:int = 18;
- var iconPointerHeight:int = 32;
- var iconLineBackgroundColor:int = 0xFF000000;
- var iconLineStyleThickness:int = 2;
- var iconBackgroundColor:uint = standardBackgroundColor;
- var lineColor:uint = standardLineColor;
- // Create a shape:
- var iconShape:Shape = new Shape();
- // NEXT: Draw the line to the bottom left:
- iconShape.graphics.beginFill(iconBackgroundColor);
- iconShape.graphics.lineStyle(iconLineStyleThickness, lineColor);
- iconShape.graphics.moveTo(iconPointerStartX, iconBarLocation);
- iconShape.graphics.lineTo(0, iconBarHeight + iconPointerHeight);
- iconShape.graphics.lineTo(iconPointerEndX, iconBarLocation);
- iconShape.graphics.lineTo(iconPointerStartX, iconBarLocation);
- iconShape.graphics.endFill();
- // NEXT: draw the bar:
- iconShape.graphics.beginFill(iconBackgroundColor);
- iconShape.graphics.lineStyle(iconLineStyleThickness, lineColor);
- iconShape.graphics.drawRoundRect(0, iconBarLocation-(iconBarHeight/2), iconBarWidth, iconBarHeight, 3, 3);
- iconShape.graphics.endFill();
- // Create the text for the pinName:
- var textfield:TextField = new TextField();
- var outline:GlowFilter = new GlowFilter(0x00c0c0c0,1.0,2,2,4);
- outline.quality = BitmapFilterQuality.MEDIUM;
- textfield.filters=[outline];
- textfield.text = pinName;
- textfield.setTextFormat(new TextFormat("Verdana", 12, 0x00000000));
- var bitmapdata:BitmapData = new BitmapData(iconTextWidth, iconTextHeight, true, 0x00000000);
- bitmapdata.draw(textfield);
- // Draw the bitmap onto the shape graphics:
- iconShape.graphics.beginBitmapFill(bitmapdata);
- iconShape.graphics.lineStyle();
- iconShape.graphics.drawRect(0, 0, iconTextWidth, iconTextHeight);
- iconShape.graphics.endFill();
- return iconShape;
- }
- }
- }
Add Comment
Please, Sign In to add comment