How to obtain GPS satellite navigation and locate, solve getlastknownlocation, and return null

Source: Internet
Author: User

When I used android to develop map navigation, I don't know if you have ever used the location returned by the getlastknownlocation () function but it always shows null. I have encountered this problem in the past few days, girl's... It's so painful to torture me for a day ~~, Fortunately, after a day of surfing the Internet, I found documents and tried to solve the problem... Call ~~; Okay, you don't have to talk nonsense .. Directly add the code.

Before starting the code, we should first explain that if you want to test on the simulator and use mapview, you must apply for a Google Map key. There are many methods to apply for it online, I will not go into details here .. Add the following content to the androidmanifest. xml file:

Add <uses-library Android: Name = "com. Google. Android. Maps"/> to the application:

Okay... The following is the activity code ..

First, create a class inheritance and mapactivity .. Note that mapactivity must be inherited .. Other activities do not work .. Note

The following describes how to instantiate various attributes:

First, instantiate locationmanager:

Check whether the GPS function is enabled

Get the best filter ^ I call him a filter ..

Note: The location obtained through getlastknownlocation () may be null. Let alone let him go.

Note: The above mapview. displayzoomcontrols (true); must be added .. Otherwise, the map cannot be dynamically updated to display the longitude and latitude changes.

After registering the locationlistener, We will rewrite the onpause () and onresume () of the activity ()

The above GPS monitoring only listens to the changes in the status of the GPS that has been enabled, that is, the GPS has been enabled, but is it visible in the enabled status? Or in the enabled status... And so on.

We also need to register a GPS listener for the locationmanager to monitor whether the GPS has been successfully started.

Note that you can encapsulate the following part into an internal class form. Of course, you can also write it into the oncreate function ~~

Set the Map Display Mode and zoom-in button for the code in oncreate.

Next, this part can be no longer written in oncreate. It is encapsulated into a function call to obtain the best provider .. That is, the self-defined filter .. Heheh

Okay .. The code is all written .. The next step is to run and view the results .. These codes are of Version 2.3.

Test ....

Enter a latitude and longitude in ddms and click send. The following map is displayed.

I don't know where this is ~~ Haha

Changing the longitude and latitude

The map will change

Look .... The map has changed ^... Because the course is going to take place, the first thing to do is to flash ~~~

Since I wrote my blog for the first time, please do not laugh when any great god has ever flown ......

Please kindly advise

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.