简体   繁体   中英

How can i get latitude and longitude of my android device

I am working in Android. i have made a program to get latitude and longitude.

this is my program

package com.ram.currentlocation;

import android.app.Activity;    
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class Location_Gps extends Activity

{

    TextView ll;
    TextView lt;

    static double lati;
    static double longi;

/** Called when the activity is first created. */

 @Override



public void onCreate(Bundle savedInstanceState)
{

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);


  /* Use the LocationManager class to obtain GPS locations */

LocationManager mlocManager = 

(LocationManager)getSystemService(Context.LOCATION_SERVICE);

  LocationListener mlocListener = new MyLocationListener();



 mlocManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);
  }



 /* Class My Location Listener */

    public class MyLocationListener implements LocationListener


    {

    @Override

    public void onLocationChanged(Location loc)

    {


        lati= loc.getLatitude();

        longi=loc.getLongitude();

    String Text = "My current location is:" +

    "Latitud = " + loc.getLatitude() +"Longitud = " + loc.getLongitude();


    Toast.makeText( getApplicationContext(), Text,  Toast.LENGTH_SHORT).show();


       }


    @Override

    public void onProviderDisabled(String provider)

    {

    Toast.makeText( getApplicationContext(),"Gps Disabled",   Toast.LENGTH_SHORT ).show();

    }


    @Override

    public void onProviderEnabled(String provider)

    {

    Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();

    }


    @Override

    public void onStatusChanged(String provider, int status, Bundle extras)

    {


    }
    public Location_Gps getLocationCordinates()
    {
          Location_Gps location_Gps =new Location_Gps();

        return location_Gps;

    }

    }
    //---



}

but for the first time it gives latitude and longitude 0,0. whenever i move my device then it gives some points. please suggest me what should i do get the ordinates at first time.

for the first time you can use getLastKnownLocation feature like this:

   Location locations = _locationManager.getLastKnownLocation(_provider);
   List<String>  providerList = _locationManager.getAllProviders();
   if(null!=locations && null!=providerList && providerList.size()>0) {
   double _longitude = locations.getLongitude();
   double _latitude = locations.getLatitude();
   }

You can't get GPS coordinates at once in Android. Only if someone else has requested them right before you, or if your GPS module is turned on for some time, then you might have a chance to get them on the first call. So, it's regular practice to listen to GPS module until you get coordinates of a good accuracy.

Refer to oficial android documentation for code examples: http://developer.android.com/guide/topics/location/obtaining-user-location.html

Yopu have ti utilize location service(s) - there are a lot of them.
There are specific implicationf for each of available location services, like energy consumption, startup time, coarsenes etc. I found blog entry by Reto Maier useful: http://android-developers.blogspot.com/search/label/Location

This is a known problem and is due to silly mistakes done by the GPS chipset engineers. Also note that the starting few points almost always are known to wobble a bit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM