Android GPS Positioning implementation (2-1) using Google Maps

Source: Internet
Author: User

To display Google Maps on the android client, you need to use Google's API. This time, Google is directly used instead of the android SDK. Because there is no detail, how many differences are there between them, it is not clear yet. Wait for a while and take a closer look. The view shown on the map is:Com. google. android. maps. if mapview is to be used, you have to apply for a map key from Google. I will not elaborate on how to apply for a map key here, but I personally feel that if it is a test, you can find a key without knowing what the difference is.

After the preceding steps are completed, because the map uses a network, you must have the network access permission, which must be added. Otherwise, the map information cannot be obtained.

The following is the implementationCode:

First, define the map controller:

 
  PrivateMapcontroller;

PrivateGeopoint; // This indicates that the positioning center is implemented in the code.

 

Then we can implement it!

 

The following is the implementation code:

Code

  
Setcontentview (R. layout. Main );

Mapview = (Mapview) findviewbyid (R. Id. mapview1 );

Mapcontroller = Mapview. getcontroller ();
Mapview. setenabled ( True );
Mapview. setclickable ( True );
Mapview. setbuiltinzoomcontrols ( True );

Geopoint = New Geopoint (( Int ) 40.9166666666667 * 1000000 ,( Int ) 116.816666666667 * 1000000 );
Mapcontroller. animateto (geopoint );
Mapcontroller. setzoom ( 12 );

 

If you want to display information at a certain point, you can use the following function to set it:

 

Code

  Class  Mylocationoverlay  Extends  Overlay
{
@ Override
Public Boolean Draw (canvas, mapview, Boolean Shadow, Long When)
{
Super . Draw (canvas, mapview, shadow );
Paint paint = New Paint ();
Point myscreencoords = New Point ();
// Convert longitude and latitude to actual screen coordinates
Mapview. getprojection (). topixels (geopoint, myscreencoords );
Paint. setstrokewidth ( 1 );
Paint. setargb ( 255 , 255 , 0 , 0 );
Paint. setstyle (paint. style. Stroke );
Bitmap BMP = Bitmapfactory. decoderesource (getresources (), R. drawable. Home );
Canvas. drawbitmap (BMP, myscreencoords. X, myscreencoords. Y, paint );
Canvas. drawtext ( " Name to be displayed " , Myscreencoords. X, myscreencoords. Y, paint );
Return True ;
}
}

 

Of course, to display a map, if we inherit activity, it cannot be displayed, so we need to change it to mapactivity, which is why we need to use Google API!

All right, this map, combined with the GPS information described above, has been achieved, and then the following can be achieved through GPS positioning, in fact, you only need to combine the two of them to achieve positioning!

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.