android - 需要当前位置坐标

标签 android gps location

包 com.demo.location;

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.util.Log;
import android.widget.Toast;

public class MyLocation  extends Activity
{
    LocationManager mlocManager;
    LocationListener mlocListener;
    /** 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 */
        mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

        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)
        {
            Log.e("","HERE It IS");
            loc.getLatitude();
            loc.getLongitude();

            Log.e("","LAT"+loc.getLatitude());
            Log.e("","LONG"+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)
        {

        }
    }
}



Manifest File

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

我只需要用户所在的确切位置。运行此应用程序后,我从模拟器和设备中没有得到任何信息...(三星 Galaxy GT S5360)。另外,我只需要纬度和经度,并且需要将其作为参数传递给其他方法,因此也欢迎任何其他解决方案。

问候,

最佳答案

使用

locManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

代替

locManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);

就我的情况而言,这对我很有帮助。

另请添加以下权限。

android.permission.INTERNET
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION
android.permission.ACCESS_MOCK_LOCATION

关于android - 需要当前位置坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11664239/

相关文章:

android - 未连接。调用 Connect 或等待 onConnected() 在 onConnected 内部调用异常?

android - 无法获取位置

android - ionic 无法运行 ionic 平台android

ios - 当我的应用程序 (phonegap 3.1) 进入后台时 GPS 关闭

android - 为 MultiUserChat 添加监听器 (aSmack)

android - 在android中立即获取位置

java - 如何将我的位置(经度和纬度)作为回复短信发送给其他人?

c++ - Xcode 在哪里创建 .txt 文件?

android - posix setuid 不同进程

java - java中如何判断数组列表是否为空?