我如何获得我的安卓设备的经纬度?

  • 本文关键字:经纬度 何获得 android
  • 更新时间 :
  • 英文 :


我在Android工作。我编写了一个程序来获取经纬度。

这是我的程序

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;
    }
    }
    //---

}

但是第一次给出经纬度0,0。每当我移动我的设备,它就会给出一些积分。请告诉我应该怎么做才能第一时间得到坐标

第一次可以这样使用getLastKnownLocation特性:

   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();
   }

你不能在Android中立即获得GPS坐标。只有当别人在你之前请求他们,或者如果你的GPS模块打开了一段时间,那么你可能有机会在第一次通话时得到他们。所以,收听GPS模块是一种常规练习,直到你得到一个很精确的坐标。

参考android官方文档中的代码示例:http://developer.android.com/guide/topics/location/obtaining-user-location.html

Yopu必须使用位置服务-有很多这样的服务。
每个可用的位置服务都有特定的含义,比如能耗、启动时间、粗化等。我发现Reto Maier的博客很有用:http://android-developers.blogspot.com/search/label/Location

这是一个已知的问题,是由于GPS芯片组工程师犯下的愚蠢错误。还要注意的是,开始的几个点几乎总是有一点摆动。

  • 检查精度是否足够好。使用getAccuracy()
  • 检查onLocationChanged(Location loc)中的loc是否为空。
  • 使用GPSStatus.Listener获取第一次修复的时间。在此之前的任何位置实际上都是无用的
  • 这是一种非常可靠的方法,可以知道是否获得了修复。

最新更新