关于此问题有一些疑问,但是我找不到任何方法都可以正常工作。我使用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(); } }


这是一个示例,实现了OnConnectionFailedListener,
    通过3个接口上方的ConnectionCallbacks,com.google.android.gms.location.LocationListener,您可以获取continueos Location。
LocationRequest类的getLastLocation方法将为您提供位置信息。
onLocationChanged方法将为您提供所需的输出。

10-05 19:50