Jee*_*eva 17 android geolocation latitude-longitude google-latitude geocode
我使用以下代码来获取当前位置即(纬度和经度),但我没有获得当前位置(纬度和经度).
谁知道为什么?
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.Toast;
public class Location_Gps extends Activity
{
/** 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.GPS_PROVIDER, 0, 0, mlocListener);
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
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)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
PS:我在manifest文件中使用以下权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_CORSE_LOCATION"/>
Run Code Online (Sandbox Code Playgroud)
evi*_*one 25
您的清单文件中有错误.正确的是:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Run Code Online (Sandbox Code Playgroud)
小智 5
您必须将位置修复发送到模拟器,因为模拟器是您需要提供的位置修复软件.
如果你正在使用eclipse去
Window -> Show view -> Other
选择Android选项卡并搜索模拟器控件.
在您看到模拟器控制窗口导航到位置控件之后.
如下图所示

发送loc修复后,您可以使用
Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Run Code Online (Sandbox Code Playgroud)
对象在哪里lm,LocationManager所以90%的工作完成;)
干杯!