0

我正在尝试在我的 android 项目中实现用户位置。我已经尝试了很多在互联网上找到的代码,但没有任何效果。Location Manager 和 Location 客户端都没有尝试过新的 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

    }

}

我等待很长时间没有得到任何位置更新,但我不知道我的错误是什么。

4

2 回答 2

0

首先在清单中添加此权限 -

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

您可能还想添加ACCESS_COARSE_LOCATIONGPS 不可用时的权限,并使用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();
}
于 2014-08-23T15:48:05.437 回答
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();
}

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

于 2014-08-23T16:01:35.637 回答