Solved

Writing Actionscript code

Posted on 2011-09-09
4
316 Views
Last Modified: 2012-05-12
Utilizing the following code to access the Google Maps API, How would I write a program to do the following:

1. Have the user select a set of pre-determined coordinates (arrival and destination)  from drop down lists
2. Calculate and display the distance between the locations
3. Calculate mileage
4. Calculate total cost of trip based on such things as food allowance and parking fees.
I am extremely new to actionscript/flash and would like to develop an application that would allow the user to do this for an organization with over 600 locations.
import com.google.maps.LatLng;
import com.google.maps.Map;
import com.google.maps.MapEvent;
import com.google.maps.MapType;
import com.google.maps.InfoWindowOptions;
import com.google.maps.LatLngBounds;
import com.google.maps.MapMoveEvent;
import com.google.maps.overlays.Marker;
import com.google.maps.overlays.MarkerOptions;
import com.google.maps.interfaces.IPolyline;
import com.google.maps.services.*;

var dir:Directions;
var polyline:IPolyline;
var map:Map;
function setupMap()
{
	map = new Map();
	map.sensor = "true";
	map.key ="ABQIAAAAjf2cvxDuc5J1E9CrJXmy9RQrAB2acdB4QgEQtVMeGDLCPkNwtRR8XAwx0MS9MI4RYmgwJ-jCIN9QaQ";
	map.setSize(new Point(400,400));
	map.x = 0;
	map.y = 0;
	map.addEventListener(MapEvent.MAP_READY, onMapReady);
	this.addChild(map);
}
setupMap();

function onMapReady(event:Event):void
{
	dir = new Directions();
	dir.addEventListener(DirectionsEvent.DIRECTIONS_SUCCESS,onDirectionsLoaded);
	dir.load("41.496942,-87.715094 to 125 south clark, chicago, il 60603");
}
function onDirectionsLoaded(event:DirectionsEvent):void
{
	var returnedDirection:Directions = event.directions as Directions ;

	var startLatLng:LatLng = returnedDirection.getRoute(0).getStep(0).latLng;
	var endLatLng:LatLng = returnedDirection.getRoute(returnedDirection.numRoutes - 1).endLatLng;

	polyline = returnedDirection.createPolyline();

	// Remove everything from map and add back the markers and polyline 
	map.clearOverlays();
	map.addOverlay(polyline);
	map.addOverlay(new Marker(startLatLng));
	map.addOverlay(new Marker(endLatLng));
	map.setCenter(returnedDirection.bounds.getCenter(), map.getBoundsZoomLevel(returnedDirection.bounds));
}

Open in new window

0
Comment
Question by:fool5683
  • 2
  • 2
4 Comments
 
LVL 29

Accepted Solution

by:
dgofman earned 500 total points
ID: 36515262
I am using pure ActionScript if you want you can replace by Flash  components

import com.google.maps.InfoWindowOptions;
import com.google.maps.LatLng;
import com.google.maps.LatLngBounds;
import com.google.maps.Map;
import com.google.maps.MapEvent;
import com.google.maps.MapMoveEvent;
import com.google.maps.MapType;
import com.google.maps.interfaces.IPolyline;
import com.google.maps.overlays.Marker;
import com.google.maps.overlays.MarkerOptions;
import com.google.maps.services.*;

var dir:Directions;
var polyline:IPolyline;
var map:Map;
function setupMap()
{
	var holder:Sprite = new Sprite();
	var startField:TextField = createNavTextField(10);
	holder.addChild(startField);
	
	var endField:TextField = createNavTextField(startField.x + startField.width + 10);
	holder.addChild(endField);
	
	var search:SimpleButton = new SimpleButton();
	search.upState = createButtonStateState(0xcccccc);
	search.overState = createButtonStateState(0xc6c6c6);
	search.downState = createButtonStateState(0x999999);
	search.hitTestState = createButtonStateState(0x999999);
	search.useHandCursor = true;
	search.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void{
		if(dir == null){
			dir = new Directions();
		}else{
			dir.removeEventListener(DirectionsEvent.DIRECTIONS_SUCCESS,onDirectionsLoaded);
		}
		dir.addEventListener(DirectionsEvent.DIRECTIONS_SUCCESS,onDirectionsLoaded);
		dir.load(startField.text + " to " + endField.text);
	});
	
	var format:TextFormat = new TextFormat(); 
	format.align = TextFormatAlign.CENTER; 
	var label:TextField = new TextField();
	label.defaultTextFormat = format; 
	label.selectable = false;
	label.mouseEnabled = false;
	label.text = "Search";
	label.width = search.width;

	var sprite:Sprite = new Sprite();
	sprite.addChild(search);
	sprite.addChild(label);
	sprite.x = endField.x + endField.width + 10;
	sprite.y = endField.y;

	holder.addChild(sprite);

	map = new Map();
	map.key ="ABQIAAAAjf2cvxDuc5J1E9CrJXmy9RQrAB2acdB4QgEQtVMeGDLCPkNwtRR8XAwx0MS9MI4RYmgwJ-jCIN9QaQ";
	map.setSize(new Point(400,400));
	map.x = startField.x;
	map.y = startField.y + startField.height + 5;
	holder.addChild(map);

	addChild(holder);
}
//setupMap();

function createNavTextField(x:uint):TextField{
	var textField:TextField = new TextField();
	textField.autoSize = TextFieldAutoSize.NONE;
	textField.type = TextFieldType.INPUT;
	textField.multiline = false;
	textField.tabEnabled = true;
	textField.wordWrap = false;
	textField.border = true;
	textField.background = true;
	textField.x = x;
	textField.y = 10
	textField.width = 195;
	textField.height = 22;
	return textField;
}

function createButtonStateState(color:Number):Sprite {
	var sprite:Sprite = new Sprite;
	sprite.graphics.lineStyle(2, 0x000000);
	sprite.graphics.beginFill(color);
	sprite.graphics.drawRoundRect(0, 0, 80, 22, 10);
	sprite.graphics.endFill();
	return sprite;
}

function onDirectionsLoaded(event:DirectionsEvent):void
{
	var ONE_METER:Number = 0.000621371192; // Miles 
	var returnedDirection:Directions = event.directions as Directions;
	
	var startLatLng:LatLng = returnedDirection.getRoute(0).getStep(0).latLng;
	var endLatLng:LatLng = returnedDirection.getRoute(returnedDirection.numRoutes - 1).endLatLng;
	
	polyline = returnedDirection.createPolyline();
	
	// Remove everything from map and add back the markers and polyline 
	map.clearOverlays();
	map.addOverlay(polyline);
	map.addOverlay(new Marker(startLatLng));
	map.addOverlay(new Marker(endLatLng));
	map.setCenter(returnedDirection.bounds.getCenter(), map.getBoundsZoomLevel(returnedDirection.bounds));
	
	trace("Distance: " + Number(returnedDirection.distance * ONE_METER).toFixed(1) + " mi, " + 
		Math.round(returnedDirection.duration / 60) +  " mins");
}

Open in new window

0
 

Author Comment

by:fool5683
ID: 36515860
I know that I may be displaying my ignorance on Flash, but I inputted the above code in adobe Flash and saved/published it as a swf file. But when I tried to open the file, all I get is a blank screen. Please advise.
0
 
LVL 29

Assisted Solution

by:dgofman
dgofman earned 500 total points
ID: 36518005
Uncomment line

 //setupMap();
0
 

Author Closing Comment

by:fool5683
ID: 36518071
I am dividing the points between the two solutions!
I will be posting other questions pertaining to this application later tomorrow. Thanks a lot Dgofman!!!!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SWF Flex project to Adobe Flash builder 5 984
FLex Calendar 3 534
AS3 Image Edit Package 3 547
Adobe Air updateFramework 3 260
First things first - Preparation We need all the part for this install and it's much nicer to have them all on hand when you need them so here's what's required. Download Eclipse 3.5 32 bit (I like the Classic flavour) from here. (http://www.e…
In this article, I show you step by step with screenshots to assist you - HOW TO: Deploy and Install the VMware vCenter Server Appliance 6.5 (VCSA 6.5), with some helpful tips along the way.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now