简体   繁体   中英

How to get user location continuosly without GPS

There are some questions regarding this issue but I could not find any method works without problem. I implemented the version of the project with GPS provider and now I want to implement a version which can find the location of user without GPS, just using network provider such as Wifi. I tried this but it is not working (I could not get the "done" message in any way):

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);

    }

...

Also, this is my manifest file:

<?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>

Can you see the problem? What you advise at this point? Is there a better way?

Dorukhan Arslan - You can use GoogleclientAPi, as locationmanager has been Deprecated -Using FusedLocationAPi it will automatically discover the provider to find the location. - https://developer.android.com/google/auth/api-client.html have a look on this - Here is one example protected void createLocationRequest() { LocationRequest mLocation = LocationRequest.create(); GoogleAPiClient mgoogle; /* protected void createLocationRequest() { LocationRequest mLocation = LocationRequest.create(); GoogleAPiClient mgoogle; /* protected void createLocationRequest() { LocationRequest mLocation = LocationRequest.create(); GoogleAPiClient mgoogle; /* enter code here * 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(); } } * 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(); } }

  • It's a sample and implementing OnConnectionFailedListener, ConnectionCallbacks, com.google.android.gms.location.LocationListener above 3 interfaces you can get the continuos Location.
  • getLastLocation Method Of LocationRequest Class will give you Location.
  • onLocationChanged Method will give you Your desired output.

您可以使用用户的IP地址获取大致位置。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM