Android Concise Development Tutorial 21: Access to the Internet draw online map

Source: Internet
Author: User

In the example Android Concise development Tutorial 17: Dialog Display Image We left an example drawmap () is not implemented, this example shows the online map, most of the current map server is the map to the image storage to improve response speed. The general size is 256x256 pixels. You can see the offline map download method analysis.

For example: URL http://www.mapdigit.com/guidebeemap/maptile.php?type=MICROSOFTMAP&x=7&y=4&z=14 display:

The following example accesses the Internet to download the map picture, and stitching into the map display, this method is also a guide Bee map development Package implementation of a basic principle.

Android apps access the Internet, you first need to give your application access to the Internet: Add in Androidmanifest.xml:

<uses-permission android:name= "Android.permission.INTERNET"/>

then implement Drawmap () as follows:

private void Drawmap () {
try {
Graphics2d.clear (Color.White);
Graphics2d.reset ();
for (int x= 6;x< 8; x + +)
{
for (int y= 3;y< 5; y++) {
String urlstring= "Http://www.mapdigit.com/guidebeemap";
urlstring+= "/maptile.php?type=microsoftmap";
urlstring+= "&x=" +x+ "&y=" +y+ "&z=14";
URL url= new URL (urlstring);
URLConnection connection=url.openconnection ();
HttpURLConnection httpconnection= (httpurlconnection) connection;
int Responsecode=httpconnection.getresponsecode ();
if (RESPONSECODE==HTTPURLCONNECTION.HTTP_OK) {
InputStream Stream=httpconnection.getinputstream ();
Bitmap Bitmap=bitmapfactory.decodestream (stream);
int []buffer= new int [Bitmap.getheight ()
* Bitmap.getwidth ()];
Bitmap.getpixels (buffer, 0, bitmap.getwidth (), 0, 0,
Bitmap.getwidth (), Bitmap.getheight ());
Graphics2d.drawimage (Buffer,bitmap.getwidth (),
Bitmap.getheight (), (x-6) * 256, (y-3) * 256);
}
}
}
Graphic2dview.refreshcanvas ();
catch (Exception e) {
}
}

The Internet classes in Android are primarily defined in the java.net.* and android.net.* packages. The results shown above are as follows:

The map does not show Mansi because the canvas size created by graphics2d does not create a full screen, the size created is 240x320, and if you create a full screen, you can display the map full screen.

See a full set of tutorials: http://www.bianceng.cn/OS/extra/201301/35252.htm

Related Article

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.