Android GPS (current location & GPS Information Update)

Source: Internet
Author: User

Recently, Android mobile app development is still very interesting. In fact, it would be very easy to develop mobile apps rather than mobile games. By mastering the main controls, you can develop simple applications.

The following describes how to use the GPS function in Android.
Because the development of GPS functions is often related to Google Map, we recommend that you first describeArticle:
[Url] http://mobiforge.com/developing/story/using-google-maps-android
[/Url]
This article explains in detail how to use the various functions of Google map in Android. This article is good and highly recommended.

After reading the above article, we will explain how to use GPS.

First, add the location service permission to androidmanifest. xml:
<Uses-Permission Android: Name = "android. Permission. access_fine_location"/>

Then let's look at the following:CodeExample:

Locationmanager locman = (locationmanager) getsystemservice (context. location_service );
Location loc = locman. getlastknownlocation (locationmanager. gps_provider );
If (loc! = NULL ){
Lat = LOC. getlatitude ();
Log. D (TAG, "latitude:" + LAT );
Lng = LOC. getlong.pdf ();
Log. D (TAG, "longpolling:" + LNG );
}

Register the locationmanager first, and then you can access getlastknownlocation to obtain the current GPS coordinates. Is it easy.
Renren
Since GPS is used, we do not just want to know the current location, but more importantly, we need to update the GPS information as the location moves. So what do we need to do?

Let's take a look at the following code example:

Locationlistener loclis = new mylocationlistener ();
Locman. requestlocationupdates (locationmanager. gps_provider, 60000, 10,
Loclis );
...
...
...
Public class mylocationlistener implements locationlistener {
@ Override
Public void onlocationchanged (location LOC ){
If (loc! = NULL ){
P = new geopoint (INT) (loc. getlatitude () * 1e6 ),
(INT) (loc. getlongpolling () * 1e6 ));
MC. animateto (P );
MC. setzoom (14 );
MC. setcenter (P );
}
}

@ Override
Public void onproviderdisabled (string provider ){
}

@ Override
Public void onproviderenabled (string provider ){
}

@ Override
Public void onstatuschanged (string provider, int status, bundle extras ){
}
}

After declaring your own locationlistener, call the requestlocationupdates method to obtain the latest GPS information.

Common Methods: Women's brand rankings
Public void requestlocationupdates (string provider, long mintime, float mindistance, locationlistener listener)

when the time exceeds mintime (unit: milliseconds), or the position moves beyond mindistance (unit: meters), the listener method is called to update the GPS information.
the official documentation provides the following description:
1. The mintime value should be no less than 60000 (that is, 1 minute), which is more efficient and energy-saving.
2. If you want to update the GPS information as real time as possible, set mintime and mindistance to 0. Risk situation Java-JavaScript

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.