java Android使用GoogleAppClient获取用户位置纬度和经度
我正在使用GoogleAppClient获取用户的纬度和经度,这就是我到目前为止所做的。我按照谷歌地图上的Udacity教程来做这件事。我做的和Udacity上显示的一模一样,但我的纬度和经度并不像instrutor上显示的那样
没有错误,所以我看不出错误在哪里。
我已经安装了Google Play Services 10.2.0版,并在构建的依赖项部分输入了该版本。gradle应用程序
非常感谢您的帮助
package com.example.udacitylocation;
import 安卓.content.pm.PackageManager;
import 安卓.location.Location;
import 安卓.support.annotation.NonNull;
import 安卓.support.annotation.Nullable;
import 安卓.support.v4.app.ActivityCompat;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.util.Log;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.widget.TextView;
import com.google.安卓.gms.common.ConnectionResult;
import com.google.安卓.gms.common.api.GoogleApiClient;
import com.google.安卓.gms.location.LocationServices;
import com.google.安卓.gms.location.LocationRequest;
public class MainActivity extends AppCompatActivity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
com.google.安卓.gms.location.LocationListener
{
private final String LOG_TAG = "LaurenceTestApp";
private TextView txtOutput;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
txtOutput = (TextView) findViewById(R.id.txtOutput);
}
@Override
public void onConnected(@Nullable Bundle bundle)
{
mLocationRequest = LocationRequest.create(); // Another way to write a new object
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(10); // Always write in milliseconds
if (ActivityCompat.checkSelfPermission(this, 安卓.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 安卓.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onLocationChanged(Location location)
{
Log.i(LOG_TAG, location.toString());
txtOutput.setText(location.toString());
}
@Override
public void onConnectionSuspended(int i)
{
Log.i(LOG_TAG, "GoogleApiClient connection has been suspended");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult)
{
Log.i(LOG_TAG, "GoogleApiClient connection has failed");
}
@Override
protected void onStart()
{
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onStop()
{
mGoogleApiClient.disconnect();
super.onStop();
}
}
XML文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:id="@+id/activity_main"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.udacitylocation.MainActivity">
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceLarge"
安卓:id="@+id/txtOutput"
安卓:text="Location Goes Here"/>
</RelativeLayout>
AndroidManifest。xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
package="com.example.udacitylocation">
<application
安卓:allowBackup="true"
安卓:icon="@mipmap/ic_launcher"
安卓:label="@string/app_name"
安卓:supportsRtl="true"
安卓:theme="@style/AppTheme">
<meta-data 安卓:name="com.google.安卓.gms.version"
安卓:value="@integer/google_play_services_version"/>
<activity 安卓:name=".MainActivity">
<intent-filter>
<action 安卓:name="安卓.intent.action.MAIN"/>
<category 安卓:name="安卓.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
<uses-permission 安卓:name="安卓.permission.ACCESS_FINE_LOCATION"/>
</manifest>
# 1 楼答案
在androidManifest中添加此internet权限。xml,我希望这对你有用。。。。 Go to this picture