我试图获得的位置只有当用户互联网是oN,而不是使用全球定位系统/网络提供商。
发布于 2015-10-07 13:08:09
你要做的是使用LocationManager.NETWORK_PROVIDER而不是LocationManager.GPS_PROVIDER来获得这个职位。NETWORK_PROVIDER将在GSM或wifi上解析。显然,在关闭wifi的情况下,将使用GSM。请记住,使用蜂窝网络基本上可以精确到500米。
http://developer.android.com/guide/topics/location/obtaining-user-location.html**
**有一些非常棒的信息和示例代码。**
在完成OnCreate()中的大部分代码之后,添加以下代码:
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);您还可以让您的活动实现LocationListener类,从而在您的活动中实现onLocationChanged()。
发布于 2015-10-07 13:04:55
尝尝这个。
public void getCurrentLocation() {
if (mlocManager != null) {
mlocManager .requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, locListener );
mlocManager .requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, locListener );
}
}发布于 2015-10-07 13:19:48
请尝试使用互联网获取当前位置。
public class CurrentLocation extends Activity {
TextView tv;
Geocoder geocoder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_current_location);
tv = (TextView) findViewById(R.id.textView1);
geocoder = new Geocoder(this);
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location
// provider.
String text = String.format(
"Lat:\t %f\nLong:\t %f\nAlt:\t %f\nBearing:\t %f",
location.getLatitude(), location.getLongitude(),
location.getAltitude(), location.getBearing());
tv.setText(text);
try {
List<Address> addressList = geocoder.getFromLocation(
location.getLatitude(), location.getLongitude(), 1);
Log.d("Current", addressList.toString());
if (addressList != null && addressList.size() > 0) {
Address address = addressList.get(0);
StringBuilder sb = new StringBuilder();
String address_own = addressList.get(0).getAddressLine(
0);
String city = addressList.get(0).getAddressLine(1);
// String country = addresses.get(0).getAddressLine(2);
Log.d("Address", address_own);
Log.d("City", city);
// Log.d("Country", country);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
sb.append(address.getAddressLine(i)).append("\n");
}
tv.append("\n" + address_own + ", " + city);
}
} catch (IOException e) {
Log.e("LocateMe", "Could not get Geocoder data", e);
}
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
// Register the listener with the Location Manager to receive location
// updates
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}https://stackoverflow.com/questions/32984134
复制相似问题