【发布时间】:2015-09-15 11:32:56
【问题描述】:
我有一个需要用户位置的应用程序,我是实施谷歌位置服务的新手。 我创建了一个单独的 java 类来处理与获取位置相关的事情,然后在活动中使用位置信息只是为了显示位置(到目前为止),但我的活动中的纬度和经度的默认值是 0。
这是实现位置相关内容的 java 类,遵循 google 指南:
import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
public class CommentLocation implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private double mLatitude;
private double mLongitude;
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
public CommentLocation(Context context){
buildGoogleApiClient(context);
}
public double getCommentLongitude() {
return mLongitude;
}
public double getCommentLatitude() {
return mLatitude;
}
protected synchronized void buildGoogleApiClient(Context context) {
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
public void onConnected(Bundle connectionHint) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
mLatitude = mLastLocation.getLatitude();
mLongitude = mLastLocation.getLongitude();
}
else{
Log.v(CommentLocation.class.getName(), "LOCATION WAS NOT RECEIVED");
}
Log.v(CommentLocation.class.getName(), "CONNECTION TO GET LOCATION IS SUCCESSFUL");
}
@Override
public void onConnectionSuspended(int i) {
//TODO
Log.v(CommentLocation.class.getName(), "CONNECTION TO GET LOCATION IS SUSPENDED");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
//TODO
Log.v(CommentLocation.class.getName(), "CONNECTION TO GET LOCATION IS FAILED");
}
}
这里是这个位置类的调用者(片段 PostCommentFragment)
public class PostCommentFragment extends Fragment {
private TextView mLatitudeText;
private TextView mLongtitudeText;
private CommentDatabase database;
private CommentLocation location;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
database = new CommentDatabase(getActivity());
location = new CommentLocation(getActivity());
setHasOptionsMenu(true);
}
@TargetApi(11) //for setDisplayHomeAsUpEnabled function
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_post_comment, container, false);
//to enable the app icon to work as a button and get the caret to appear in fragment's view
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
if(NavUtils.getParentActivityName(getActivity()) != null) {
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
mLatitudeText = (TextView) v.findViewById(R.id.latitude_text);
mLongtitudeText = (TextView) v.findViewById(R.id.longitude_text);
mLatitudeText.setText(String.valueOf(location.getCommentLatitude()));
mLongtitudeText.setText(String.valueOf(location.getCommentLongitude()));
return v;
}
我在这里做错了什么? 我还添加了这一行 compile 'com.google.android.gms:play-services:7.8.0' 到 build.gradle(Module:app)->under dependencies,并在 Manifest 中请求许可。谢谢!
【问题讨论】:
-
您是否在模拟器中启用了模拟位置?
-
我用的是真机,nexus 7 和sony xperia
标签: java android location google-api-client