在android中查找用户位置

时间:2014-08-23 13:50:41

标签: android location-services

我正在尝试在我的android项目中实现用户位置。我已经尝试了许多在互联网上找到的代码,但没有任何效果。位置管理器和位置客户端也都没有尝试过新的GoogleApiClient。但我不知道我的错误在哪里。我们是否需要任何Api密钥来获取用户位置?我保留了以下代码:      Location_Finder2.java

package com.notification.messaging;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;

import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;

public class Location_Finder2 extends Activity implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener{
    LocationRequest locationRequest;
    LocationClient locationClient;
    Location location;
    Double longitude = 0.0d;
    Double latitude = 0.0d;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        locationClient = new LocationClient(this, this, this);
        locationRequest = new LocationRequest();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(5000);
        locationRequest.setFastestInterval(1000);
    }
    @Override
    public void onLocationChanged(Location arg0) {
        locationClient.removeLocationUpdates(this);
        longitude = location.getLongitude();
        latitude = location.getLatitude();
        Log.i("New Latitude && Longitude:", "Longitude:" + longitude + ", Latitude:" + latitude);
    }

    @Override
    public void onConnectionFailed(ConnectionResult arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onConnected(Bundle arg0) {
        Log.i("Location_2","dhsjchdjcjdjcjkdjcjdn");
        location = locationClient.getLastLocation();
        if (location == null){
            locationClient.requestLocationUpdates(locationRequest, this);
        }
        else {
            latitude = location.getLatitude();
            longitude = location.getLongitude();
            Log.i("Latitude && Longitude:", "Longitude:" + longitude + ", Latitude:" + latitude);
        }
    }

    @Override
    public void onDisconnected() {
        // TODO Auto-generated method stub

    }

}

我没有等待很长时间的位置更新,但我找不到我的错误。

2 个答案:

答案 0 :(得分:0)

首先在Manifest中添加此权限 -

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

然后在New class

中添加此代码
private class MyLocationListener implements LocationListener {
//Implement Location Listener

double latitude, longitude;

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//Create instance

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);

longitude = location.getLongitude();
latitude = location.getLatitude();
//Implement LocationListener and get coordinates

    private final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            longitude = location.getLongitude();
            latitude = location.getLatitude();
        }
    }
}

您可能还想在GPS不可用时添加ACCESS_COARSE_LOCATION权限,并使用getBestProvider()方法选择您的位置提供商。

您可能还想检查GPS是否已启用 -

final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );

if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
    buildAlertMessageNoGps();
}

private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
       .setCancelable(false)
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
           public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
               startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
           }
       })
       .setNegativeButton("No", new DialogInterface.OnClickListener() {
           public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                dialog.cancel();
           }
       });
final AlertDialog alert = builder.create();
alert.show();
}

答案 1 :(得分:0)

第一眼看来,你似乎永远不会连接LocationClient。如果您没有收到Logcat消息Log.i("Location_2","dhsjchdjcjdjcjkdjcjdn");,则表明您必须在locationClient.connect();中致电onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    locationClient = new LocationClient(this, this, this);
    locationRequest = new LocationRequest();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(5000);
    locationRequest.setFastestInterval(1000);

    locationClient.connect();
}

其余代码看起来可以获得位置更新。