关于此问题有一些疑问,但是我找不到任何方法都可以正常工作。我使用GPS提供程序实现了该项目的版本,现在我想实现一个仅使用Wifi等网络提供程序就可以在没有GPS的情况下找到用户位置的版本。我尝试了此操作,但是它不起作用(我无法以任何方式获得“完成”消息):
public class MapsActivity extends FragmentActivity {
double latitude;
double longitude;
LocationManager locationManager;
android.location.LocationListener networkLocationListener;
Location location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
networkLocationListener = new android.location.LocationListener() {
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.d("->", "done.");
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, networkLocationListener);
}
...
另外,这是我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.doruk.myapplication">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but are recommended.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/orange"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".ConnectionChangeReceiver"
android:label="NetworkConnection">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
</application>
</manifest>
你看到问题了吗?您在这一点上有何建议?有没有更好的办法?
Dorukhan Arslan-您可以使用GoogleclientAPi,因为locationmanager已被弃用-使用FusedLocationAPi它将自动发现提供者以查找位置。- https://developer.android.com/google/auth/api-client.html对此一看-这里有一个例子protected void createLocationRequest() { LocationRequest mLocation = LocationRequest.create(); GoogleAPiClient mgoogle; /*
在这里输入代码 * Set the update interval */ mLocation.setSmallestDisplacement(500); // 2km mLocation.setInterval(180000);// 3 min mLocationRequest.setFastestInterval(120000);// 2 min mLocation.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); if (mgoogle == null) { mgoogle = new GoogleApiClient.Builder(this) .addApi(LocationServices.API).addConnectionCallbacks(this) .addOnConnectionFailedListener(this).build(); mgoogle.connect(); } }
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句