【问题标题】:Is there a way to get current location in a tunnel有没有办法在隧道中获取当前位置
【发布时间】:2016-07-11 05:29:04
【问题描述】:

我正在尝试获取当前位置,该代码大部分时间都有效。但是,尽管存在移动接收,但位置更新无法在隧道中工作。

我通过以下两种方法进行了尝试和追踪:

  Location NetLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  Location GPSLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

似乎都没有在隧道中更新。我与其他一些应用程序进行了交叉检查,发现位置服务在谷歌地图中正常工作。

Whole function is linked here

那么无论如何我可以在隧道中获得正确的位置更新吗?

【问题讨论】:

  • 你的意思是在隧道里没有 GPS 接收吗?确实没有。
  • @greenapps 我知道隧道中没有 GPS 接收,但谷歌地图在隧道中获取正确的位置信息,因此必须有一种方法可以获取正确的位置。问题是,该 API 是否可用于其他应用程序?
  • 谷歌地图获取正确的位置信息?从谁?没有意义。请详细说明您的意思。
  • 基站三角测量是例如一种方法。有很多方法可以做到这一点,这就是我想要找出的。
  • 这和谷歌地图有什么关系?为什么不要求对移动台进行三角测量?

标签: android geolocation


【解决方案1】:

Google 有一个名为 fusedLocationAPI 的新 API,您可以尝试使用它。这是示例代码 -

public class YourActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener  {

private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;

private static final int LOCATION_PERMISSION_CODE = 42;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_splash_screen);
    initGoogleApiClient();
}

@Override
public void onConnected(@Nullable Bundle bundle) {

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_CODE);
    } else {
        getLastKnownLocation();
    }
}

@Override
public void onConnectionSuspended(int i) {
    mGoogleApiClient.connect();
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == LOCATION_PERMISSION_CODE) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            getLastKnownLocation();
        } else {
            startLocationActivity();
        }
    }
}

private void getLastKnownLocation() {
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
}

private void initGoogleApiClient() {
    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }
}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    super.onStop();
    mGoogleApiClient.disconnect();
}
}

这只是对可以做什么的更高层次的概述。可以通过添加位置更新间隔等来进一步定制代码。您可以参考这个网站 - https://developer.android.com/training/location/retrieve-current.html 了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    相关资源
    最近更新 更多