A simple example of GPS positioning in Android _android

Source: Internet
Author: User

Today, one hours, wrote a GPS acquisition location code of a small example, including a reference to some of the code on the Web, and some changes to the code, I hope to help. The specific code is as follows: to practical adnroid platform GPS equipment, first need to add permissions, so you need to add the following permissions:

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

The specific implementation code is as follows:

First determine whether the GPS module exists or is open:

private void Opengpssettings () {
    Locationmanager alm = (Locationmanager) this
        . Getsystemservice ( Context.location_service);
    if (ALM
        . isproviderenabled (Android.location.LocationManager.GPS_PROVIDER)) {
      Toast.maketext (this, " GPS module Normal ", Toast.length_short)
          . Show ();
      return;
    }

    Toast.maketext (This, "Please open gps! ", Toast.length_short). Show ();
    Intent Intent = new Intent (settings.action_security_settings);
    Startactivityforresult (intent,0); This is returned to the Get interface when the setting is complete

  }

If turned on, it will go directly to the Display page, if it is not normal, it will go to the GPS Settings page:

Get the code as follows:

private void GetLocation ()
  {
    //Get Position Management Service
    Locationmanager Locationmanager;
    String serviceName = Context.location_service;
    Locationmanager = (Locationmanager) this.getsystemservice (serviceName);
    Find a service information criteria
    criteria = new criteria ();
    Criteria.setaccuracy (Criteria.accuracy_fine); High precision
    criteria.setaltituderequired (false);
    Criteria.setbearingrequired (false);
    Criteria.setcostallowed (true);
    Criteria.setpowerrequirement (Criteria.power_low); Low power

    String Provider = Locationmanager.getbestprovider (criteria, true);//Get GPS information
    Location Location = Locationmanager.getlastknownlocation (provider); Acquiring position
    Updatetonewlocation (location) via GPS;
    Set the listener, the minimum time for automatic Updates is interval n seconds (1 seconds for 1*1000, which is written mainly for convenience) or minimum displacement change over n m
    locationmanager.requestlocationupdates (provider, 1000,
        locationlistener);  

Here you can get the location information, but still show it, then use the following method to display:

private void Updatetonewlocation (Location Location) {

    TextView tv1;
    TV1 = (TextView) This.findviewbyid (R.ID.TV1);
    if (location!= null) {
      double latitude = location.getlatitude ();
      Double longitude= location.getlongitude ();
      Tv1.settext ("Latitude:" + latitude+ "N Longitude" + longitude);
    } else {
      Tv1.settext ("Cannot get geographic information");
    }

  

This way you can get to the current user's location, at least how to implement the map, in the following will be captured and displayed! Thanks to the people who are referencing the code!

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.