如果GPS无法使用并且没有无线,我想找到我当前的位置? 我想我需要做一些计算,或者有一些代码可以为我做。?
THX。
我写的就像那段代码,但是当我在室内时却无法找到当前的位置。
package com.anil.bestlocation;
import java.util.List;
import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.widget.TextView;
public class showBestLocation extends Activity implements LocationListener{
/** Called when the activity is first created. */
private static final String[] S = { "Out of Service",
"Temporarily Out of Service",
"Available" };
private TextView output;
private LocationManager locationManager;
private String bestProvider;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
output = (TextView) findViewById(R.id.txtLoc);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
//list all providers
List<String> providers = locationManager.getAllProviders();
for(String provider : providers){
printProvider(provider);
}
Criteria criteria = new Criteria();
bestProvider = locationManager.getBestProvider(criteria, false);
output.append("\n\nBEST PROVIDER:\n");
printProvider(bestProvider);
output.append("Location.. (initialized with the last known location):");
Location location = locationManager.getLastKnownLocation(bestProvider);
printLocation(location);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
locationManager.removeUpdates(this);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
locationManager.requestLocationUpdates(bestProvider, 20000, 1, this);
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
printLocation(location);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
output.append("\n\nSProvider Disabled: " + provider);
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
output.append("\n\nProvider Enabled: " + provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
output.append("\n\nProvider Status Changed: " + provider +
", Status= " + S[status]);
}
private void printProvider(String provider){
LocationProvider info = locationManager.getProvider(provider);
output.append(info.toString() + "\n\n");
}
private void printLocation(Location location){
if(location == null){
output.append("\nLocation UNKNOWN\n\n");
}
else{
output.append("\n\nLatitude: " + location.getLatitude() + "\nLongitude: " + location.getLongitude());
}
}
}
答案 0 :(得分:1)
http://developer.android.com/guide/topics/location/obtaining-user-location.html
更具体: http://developer.android.com/guide/topics/location/obtaining-user-location.html#BestPerformance
答案 1 :(得分:0)
在室内,您可以使用NetworkProvider获取位置。此方法使用移动蜂窝塔信息和附近的wifi基站信息的组合来近似您的位置。
这样做的方法是扫描无线基站的区域,并从附近的手机信号塔收集移动运营商的手机信号塔信息。 然后将所有这些数据发送到服务器并要求服务器估算您的位置。
如果您无法访问互联网(即:没有WiFi连接且没有移动数据连接),那么您将无法获得任何位置信息。你无能为力。这就是它的工作方式。
答案 2 :(得分:0)
您需要某种输入来检索位置。您的计算必须基于某些事情。根据您的手机硬件,您可以尝试使用蓝牙,NFC或尝试并检测发射器发出的某些频率(必须放置在该位置)。我希望这会对你有所帮助。