gpt4 book ai didi

java - Google map api 不显示我当前的位置

转载 作者:太空宇宙 更新时间:2023-11-04 12:11:44 26 4
gpt4 key购买 nike

我遵循了一些文档,但它仍然没有显示我当前的位置,这是我的代码:

package com.example.stewiesh.uniguide;

import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class Routes extends FragmentActivity implements OnMapReadyCallback
{
private GoogleMap mMap;

protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_routes);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

public void onMapReady(GoogleMap googleMap)
{
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
System.out.println("Permission error");
return;
}
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener()
{
public boolean onMyLocationButtonClick()
{
System.out.println("button error");
return false;
}
});
}

}

我正在使用最新版本的 api, list 和 gradle 中的所有内容都很好,我检查了几次......

最佳答案

我已经检查过,您的代码工作正常。

检查一下,也许在模拟器中(例如,如果您使用 Genymotion)您没有打开 GPS,这就是为什么它不显示您的位置...

这是工作代码:

@Override
public void onMapReady(GoogleMap googleMap) {
Log.d(TAG, "onMapReady()");
mGoogleMap = googleMap;
/*
* Adding map control buttons
* */
mUiSettings = mGoogleMap.getUiSettings();
mUiSettings.setZoomControlsEnabled(true);

if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "PERMISSION_GRANTED");

/*
* Allow application to get user location
* */
mGoogleMap.setMyLocationEnabled(true);



} else {
Log.d(TAG, "PERMISSION_NOT_GRANTED");
Toast.makeText(getActivity(), "Permissions to get the location are not granted. Please setup the permissions.", Toast.LENGTH_LONG).show();
}
}

关于java - Google map api 不显示我当前的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39799304/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com