Fgmap learning-add your own map data

Source: Internet
Author: User
Today, let's go to our question: how can we load our map data? The data used here can be generated by ourselves, downloaded from the Internet, and stored on the local machine or on our own server, you only need to modify the corresponding path.

Add the following in the previous example:

Right-click the src directory of the project, select "New ActionScript class", and enter the package name, class name, And superclass:

After you click "finish ",ProgramCreate a mytilelayer. As fileCodePaste it into the file:

Package COM. examples {import COM. fgmap. maps. color; import COM. fgmap. maps. copyright; import COM. fgmap. maps. copyrightcollection; import COM. fgmap. maps. latlng; import COM. fgmap. maps. latlngbounds; import COM. fgmap. maps. tilelayerbase; import COM. fgmap. maps. interfaces. icopyrightcollection; import COM. fgmap. maps. interfaces. IMAP; import flash. display. displayobject; import flash. display. loader; import flash. display. lo Aderinfo; import flash. events. *; import flash. geom. point; import flash.net. URLRequest; public class mytilelayer extends tilelayerbase {private var mapminzoom: Int = 11; // minimum display level private var mapmaxzoom: Int = 13; // maximum display level public function mytilelayer (tilesize: number) {var copyrightcollection: copyrightcollection = new copyrightcollection (); Super (copyrightcollection, mapminzoom, mapmaxzoom, 1); // call the method of the parent class // create your own version Permission description copyrightcollection. addcopyright (New Copyright ("mycopyright", new latlngbounds (New latlng (-180,-90), new latlng (180, 90), 0, "This is my own version description");} // overwrite the method for Loading map data. This is very important. The map data is read from override public function loadtile (tilepos: Point, zoom: Number): displayobject {var testloader: loader = new loader (); var Ymax: Int = 1 <zoom; var y: Int = Ymax-tilepos. y-1; var URLRequest: URLRequest; If (zoom <mapminzoom) | (Zo Om> mapmaxzoom) {URLRequest = new URLRequest ("assets/tiles/nomap.png "); // content displayed when no map exists} else {URLRequest = new URLRequest ("assets/tiles/" + zoom + "/" + tilepos. X + "/" + Y + ". PNG "); // The path where the map is stored. It can be local or server address .} Testloader. contentloaderinfo. addeventlistener (ioerrorevent. io_error, ioerrorhandler); testloader. load (URLRequest); Return testloader;} // error handling private function ioerrorhandler (Event: ioerrorevent): void {trace ("ioerrorhandler:" + Event );}}}

Use the class we just wrote in The fgmapdemo. mxml file:

<? XML version = " 1.0 " Encoding = " UTF-8 " ?>
< 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 "
Minwidth = " 600 " Minheight = " 400 "
Xmlns: Maps = " Com. fgmap. Maps .* " >
< FX: Declarations >
<! -- Place non-visual elements (such as services and value objects) here -->
</ FX: Declarations >

maps: map id = " Map " width = " 100% " height = " 100% " mapevent_mapready = " onmapreadyhandler (Event) " />

< FX: script >
<! [CDATA [
Import Com. fgmap. Maps. * ;
Import Com. fgmap. Maps. mapmouseevent;
Import Com. fgmap. Maps. Controls. maptypecontrol;
Import Com. fgmap. Maps. Controls. navigationcontrol;
Import Com. fgmap. Maps. Controls. overviewmapcontrol;
Import Com. fgmap. Maps. Controls. scalecontrol;
Import Com. fgmap. Maps. Interfaces. imaptype;
Import Com. fgmap. Maps. overlays. * ;
Import Com. Examples. mytilelayer;

PrivateVaR marker: marker;

PrivateVaR centrelatlng: latlng= NewLatlng (39.911842984749946,116.400146484375);//A Coordinate Position in Beijing.

// How to execute a map after it is loaded
Protected Function onmapreadyhandler (Event: mapevent ): Void
{
Map. enablecontinuouszoom (); // Enable continuous smooth scaling.
Map. enablescrollwheelzoom (); // Enable scroll wheel scaling.
Map. addcontrol ( New Maptypecontrol ()); // Button that allows you to switch between map types.
Map. addcontrol ( New Navigationcontrol ()); // You can change the navigation parameters of a map, including the zoom level, center position, and spatial azimuth.
Map. addcontrol ( New Scalecontrol ()); // The scale control is a visual indicator used to indicate the resolution and zoom level of the current map.

Map. setcenter (centrelatlng, 11 ); // Set the central point of the map.

Marker = New Marker (centrelatlng ); // Create a annotation.
Map. addoverlay (Marker ); // This annotation is displayed on the map.

VaR normalmaptype: imaptype = Maptype. normal_map_type; // Define a map type
VaR tilelayers: Array = New Array ();
Tilelayers. Push ( New Mytilelayer (normalmaptype. gettilesize ())); // Map type
VaR mymaptype: imaptype = New Maptype (tilelayers, normalmaptype. getprojection (), " My maps " ); // Create your own map type
Map. addmaptype (mymaptype ); // Add to map
Map. setmaptype (mymaptype ); // Set your own map visibility
}

]>
</FX: script>
</S: Application>

After the program is completed, we run the program. The result is:

In this way, we can add our own map.

At this point, our program is finished, but we still need some preparation:

Map image, I put it in"Assets/tiles"Directory, the data is generated using my graph cutting tool.You can try other data. Remember to change the method for obtaining the image address!

Come on, oursFgmap Library FileI also changed it. Now I upgraded it to 1.01 and put it in the lib directory.

For the complete sample program, click here to download: http://files.cnblogs.com/liongis/FGMapDemo2.rar

If you have any questions, contact me:

Mail: liongis@163.com

QQ: 1366940902

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.