2015-11-05 119 views
-1

我开发了一个android应用程序,其中gps随时打开。现在我怎么能找到用户的经度和纬度。请帮助我如何查找GPS上的用户的经度和纬度

+4

可能的复制[我如何获得当前GPS位置编程中的Android?( http://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android) –

回答

0

要通过位置提供者访问当前位置信息,我们需要使用android清单文件设置权限。

<manifest ... > 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission. ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
</manifest> 

当我们为我们的Android应用程序使用网络位置提供程序时,会使用ACCESS_COARSE_LOCATION。但是,ACCESS_FINE_LOCATION正在为这两个提供商提供权限。 INTERNET权限是必须为网络提供者使用的。

创建的LocationManager实例作为参考位置服务

对于任何背景的Android服务,我们需要使用它得到参考。同样,位置服务引用将使用getSystemService()方法创建。该引用将与新创建的LocationManager实例一起添加,如下所示。

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

请求当前位置从的LocationManager

创建位置服务参考后,位置更新使用requestLocationUpdates的LocationManager()方法请求。对于此功能,我们需要发送位置提供程序的类型,秒数,距离和要更新位置的LocationListener对象。

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 

从LocationListener的上位置

LocationListener的将基于指定的距离间隔或数秒被通知的变化,接收位置更新。

本示例使用GPS提供程序提供当前位置更新。整个Android应用程序代码如下,在如下图所示

public class MainActivity extends Activity implements LocationListener{ 
protected LocationManager locationManager; 
protected LocationListener locationListener; 
protected Context context; 
TextView txtLat; 
String lat; 
String provider; 
protected String latitude,longitude; 
protected boolean gps_enabled,network_enabled; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
txtLat = (TextView) findViewById(R.id.textview1); 

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
} 
@Override 
public void onLocationChanged(Location location) { 
txtLat = (TextView) findViewById(R.id.textview1); 
txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude()); 
} 

@Override 
public void onProviderDisabled(String provider) { 
Log.d("Latitude","disable"); 
} 

@Override 
public void onProviderEnabled(String provider) { 
Log.d("Latitude","enable"); 
} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 
Log.d("Latitude","status"); 
} 
} 

布局和Android清单XML文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:context=".MainActivity" > 

     <TextView 
      android:id="@+id/textview1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true" 
      android:text="@string/hello_world" /> 

    </RelativeLayout> 
相关问题