GPS gets Location to obtain the longitude and latitude of the Location Point, gpslocation

Source: Internet
Author: User

GPS gets Location to obtain the longitude and latitude of the Location Point, gpslocation

Use your mobile phone to obtain the longitude and latitude of the location:

Location is often used in Android development. For example, you can obtain the weather by longitude and latitude, and obtain the detailed Address of the region based on Location (such as Google Map development. In Android, Location manager is used to obtain Location. Generally, Location is obtained by GPS, and Location is obtained by WIFI. The following describes how to obtain Location by GPS:

Step 1:

Create an Android project named GPS

 

Step 2:

Use LocationManager in MainActivity to obtain the longitude and latitude:

Import java. util. List;

Import android. app. Activity;
Import android. content. SharedPreferences;
Import android. content. SharedPreferences. Editor;
Import android. location. Criteria;
Import android. location. Location;
Import android. location. LocationListener;
Import android. location. LocationManager;
Import android. OS. Bundle;

Public class MainActivity extends Activity {
Private LocationManager manager;
Private SharedPreferences sp;

Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

Manager = (LocationManager) getSystemService (LOCATION_SERVICE); // gets the mobile phone location information

List <String> providers = manager. getAllProviders ();

// For (String provider: providers ){
// System. out. println (provider );
//}

// Obtain conditions
Criteria criteria = new Criteria ();
Criteria. setAccuracy (Criteria. ACCURACY_FINE); // get the exact location
Criteria. setCostAllowed (true); // overhead is allowed.
Criteria. setPowerRequirement (Criteria. POWER_HIGH); // If the consumption is high, the acquisition frequency is high.
Criteria. setSpeedRequired (true); // mobile phone location
Criteria. setAltitudeRequired (true); // altitude

// Obtain the best provider: gps on the mobile phone or Simulator
String bestProvider = manager. getBestProvider (criteria, true); // use a GPS satellite

System. out. println ("the best provider is" + bestProvider );
Sp = getSharedPreferences ("config", MODE_PRIVATE );

// Parameter: 1. provider 2. Obtain the parameter value at intervals of time. 3. specify the number of meters at intervals. 4. The Listener triggers the callback function.
Manager. requestLocationUpdates (bestproviders, 60000,100, new MyLocationListener ());
}

Private class MyLocationListener implements LocationListener {
/**
* Mobile phone location changed
*/
Public void onLocationChanged (Location location ){
Location. getAccuracy (); // Accuracy
String latitude = location. getLatitude () + "; // longitude
String longpolling = location. getlongpolling () + ""; // latitude

// Save the obtained latitude and longitude information to SharedPreferences
Editor editor = sp. edit ();
Editor. putString ("lastloaction", latitude + "-" + longpolling );
Editor. commit ();

}

/**
* When the provider status changes at a location
*/
Public void onStatusChanged (String provider, int status, Bundle extras ){

}


/**
* When a device is enabled
*/
Public void onProviderEnabled (String provider ){

}


/**
* When a device is disabled
*/
Public void onProviderDisabled (String provider ){

}

}
}

Step 3:

Configure AndroidManifest. xml as follows (add Location-related permissions ):

 

<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "cn. itcast. gps"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">

<Uses-sdk android: minSdkVersion = "8"/>
<Uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION"/>
<Uses-permission android: name = "android. permission. ACCESS_COARSE_LOCATION"/>
<Uses-permission android: name = "android. permission. ACCESS_MOCK_LOCATION"/>

<Application
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name">
<Activity
Android: name = ". DemoActivity"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>

<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
</Application>

Step 4:

Deploy the application. You can add Log. I () to the code and observe the printed information in LogCat.

Or export the data/<packagename>/pai_pre/config. xml file directly.

 

You can query the longitude and latitude on your website.

 

Note:

Android2.3 has a bug. No prompt is displayed after positioning.

2.3 The simulator does not support sending simulated longitude and latitude manually

2.2 simulator available


Why is the location obtained by android gps always blank?

Because your GPS Cannot find satellites.

Obtain the address through latitude and longitude

Do you know the class reference of Google APIs? What is the use of others? Tell you, and you forget. You open the class reference and view the object functions related to the query information, then, let's look at the subsequent explanations. We will not solve this problem, but also have a sense of accomplishment and a good habit, so that we can develop better in the future.

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.