Java 在android中获取当前位置(坐标)?

Java 在android中获取当前位置(坐标)?,java,android,Java,Android,我正在尝试获取当前位置的经度和纬度坐标。以下是我目前的代码: public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LocationMana

我正在尝试获取当前位置的经度和纬度坐标。以下是我目前的代码:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    MyLocationListener myLocationListener = new MyLocationListener();
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, myLocationListener);

}
}

还有这个班:

public class MyLocationListener implements LocationListener {
private static final String TAG = "COORDINATES: ";

@Override
public void onLocationChanged(Location location) {
    if(location != null){
        Log.e(TAG, "Latitude: " + location.getLatitude());
        Log.e(TAG, "Longitude: " + location.getLongitude());
    }
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}
}


当我在emulator中运行应用程序时,我不会得到任何带有坐标的日志消息。有什么建议吗?

最好的方法是使用Google Play Services library提供的最新FusedLocationApi

如果您想使用旧方法,这很好,但可能无法获得非常准确的结果

无论哪种方式,请确保您在android清单中启用了internet权限,无论是粗略位置还是精细位置,或者两者都启用

另外,如果您有android 6.0,请记住您必须请求运行时权限,否则它将不适用于您

我昨天回答了一个类似的问题,你可以发现——哪一个有效

还有一个链接指向FusedLocationApi的示例代码

我希望这对你有帮助,祝你好运

更新

您可以将Google Play服务添加到build.gradle中,如下所示:

compile 'com.google.android.gms:play-services:9.2.'
但是,如果您只对一项服务感兴趣,如位置,您可以具体说明:

compile 'com.google.android.gms:play-services-location:9.2.1'
注意


我强烈建议您不要在UI线程上获取用户位置,因为从长远来看,这会破坏用户体验!使用单独的线程

最好的方法是使用Google Play Services库提供的最新FusedLocationApi

如果您想使用旧方法,这很好,但可能无法获得非常准确的结果

无论哪种方式,请确保您在android清单中启用了internet权限,无论是粗略位置还是精细位置,或者两者都启用

另外,如果您有android 6.0,请记住您必须请求运行时权限,否则它将不适用于您

我昨天回答了一个类似的问题,你可以发现——哪一个有效

还有一个链接指向FusedLocationApi的示例代码

我希望这对你有帮助,祝你好运

更新

您可以将Google Play服务添加到build.gradle中,如下所示:

compile 'com.google.android.gms:play-services:9.2.'
但是,如果您只对一项服务感兴趣,如位置,您可以具体说明:

compile 'com.google.android.gms:play-services-location:9.2.1'
注意


我强烈建议您不要在UI线程上获取用户位置,因为从长远来看,这会破坏用户体验!使用单独的线程

您必须在Emulator中模拟位置。您可以通过访问Android设备管理器并选择Emulator Control选项卡并将位置发送到Emulator来完成此操作


您必须在Emulator中模拟位置。您可以通过访问Android设备管理器并选择Emulator Control选项卡并将位置发送到Emulator来完成此操作


谢谢你的回答,我有一个简短的问题。当你说Google Play Services时,是不是在Google开发者控制台中被称为“Google Play Game Services”?(很抱歉,这是新手)
FusedLocation API
也使用
gps
进行精确定位,并使用网络进行
rough\u定位
因此,使用gps时,位置几乎与内部使用的
FusedLocation
标志相同gps@Eenvincible依赖项下?“build.grade(Module:app)”是的,依赖项,很抱歉我忘了添加它!如果你需要进一步的帮助,让我知道我可以chat@Eenvincible嗨,我能和你聊一聊吗?我正在尝试实施您的解决方案,但我似乎无法使其生效。谢谢您的回答,我有一个简短的问题。当你说Google Play Services时,是不是在Google开发者控制台中被称为“Google Play Game Services”?(很抱歉,这是新手)
FusedLocation API
也使用
gps
进行精确定位,并使用网络进行
rough\u定位
因此,使用gps时,位置几乎与内部使用的
FusedLocation
标志相同gps@Eenvincible依赖项下?“build.grade(Module:app)”是的,依赖项,很抱歉我忘了添加它!如果你需要进一步的帮助,让我知道我可以chat@Eenvincible嗨,我能和你聊一聊吗?我正在尝试实现您的解决方案,但我似乎无法使其工作。emulator不像phone那样工作。他们没有GPS或网络,尽管你可以用一些工具和设置来模拟地理坐标。模拟器不像手机。他们没有GPS或网络,尽管你可以用一些工具和设置来模拟地理坐标。