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.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)
{
}
}
}
PS:我在 manifest
文件中使用以下权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_CORSE_LOCATION"/>
I am using the following code to get the current location namely (latitude and longitude), but I am not getting current location (latitude and longitude).
Anyone knows why?
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)
{
}
}
}
P.S.: I am using the following permissions in the manifest
file:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_CORSE_LOCATION"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的清单文件中有错误。正确的一项是:
You have a mistake in your manifest file. Correct one is:
您必须将位置修复发送到模拟器,因为模拟器是您需要提供位置修复的软件。
如果您使用的是 Eclipse,请转到
Window ->显示视图->其他
选择 Android 选项卡并搜索模拟器控件。
看到模拟器控制窗口后,导航到位置控件。
如下图所示
发送 loc 修复后,您可以使用
Where
lm
是LocationManager
对象,因此 90% 的工作已完成;)干杯!
You have to send location fix to emulator,as emulator is software you need to provide with location fix.
If you are using eclipse go to
Window -> Show view -> Other
Select Android tab and search for emulator control.
After u see emulator control window navigate to location controls.
As u see in below pic
After you have sent loc fix you can use
Where
lm
isLocationManager
object so 90% of work is done ;)Cheers!
在你的android清单中你设置了权限吗?
就仅获得 (0,0) 坐标而言,您可能正在使用模拟器。如果您使用的是 eclipse,请转到模拟器控件,在底部您可以将假坐标发送到设备
In your android manifest did you set the permissions?
As far as only getting (0,0) co-ords you're probably using the emulator. If you're using eclipse go to the emulator control and at the bottom you can send the fake co-ords to the device
您没有使用正确的权限。正确的是:
顺便说一句,您的需求只能通过以下方式满足:
所以不需要
ACCESS_COARSE_LOCATION
You are not using the correct permission. The correct one is:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
By the way, your demand can only be fulfilled by:
<using-permission android:name="ACCESS_FINE_LOCATION" />
So there is no need for
ACCESS_COARSE_LOCATION