【发布时间】:2016-05-25 03:39:08
【问题描述】:
我正在尝试实现谷歌地图 API。我想在地图上看到我自己的位置,并且我希望它在我显着移动时发生变化。问题是没有错误,但它没有在地图上显示我的位置标记。这是我在 .java 文件中的 onCreate() 方法
private GoogleMap mMap;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
public static final String TAG = MapPane.class.getSimpleName();
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_pane);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
// Create the LocationRequest object
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000) // 10 seconds, in milliseconds
.setFastestInterval(1 * 1000); // 1 second, in milliseconds
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}//onCreate
这是我认为我在这里缺少一些东西的方法
public void onConnected(@Nullable Bundle bundle) {
Log.i(TAG, "Location services connected.");
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
Log.i(TAG, "loc exists");
if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
|| checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}//if
}//if
else {
handleNewLocation(location);
}//else
}//onConnected
因为我从来没有进入过第二个 if 语句。我想我首先需要用户的许可?在我的设备上手动打开 gps 也不起作用。以及下面我用于在地图上显示标记的方法
private void handleNewLocation(Location location) {
Log.d(TAG, location.toString());
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
LatLng latLng = new LatLng(currentLatitude, currentLongitude);
MarkerOptions options = new MarkerOptions()
.position(latLng)
.title("I am here!");
mMap.addMarker(options);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}//handleNewLocation
我使用了更多的方法,但我认为那里没有缺陷。在我包含的 AndroidManifest.xml 中
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
【问题讨论】:
-
位置的价值是什么?
-
它为空,因为我可以看到 "Log.i(TAG, "loc exists");"在我的 Android 监视器中
-
你是否像本教程developer.android.com/training/location/retrieve-current.html一样添加了onStart()和onStop()方法?
-
您的控制台是否出现任何错误?
-
这是我得到的唯一错误:“加载模块描述符类失败:找不到类”com.google.android.gms.dynamite.descriptors.com.google.android.gms. googlecertificates.ModuleDescriptor" "
标签: android google-maps android-studio