问题描述
我试图通过Google地图上的位置按钮获取当前位置,但我遇到了一些问题。有了新的运行时权限(API级别23),我不知道自己做得好不好。
使用下面的代码,当我使用虚拟设备API 23时,位置按钮不会出现,并且虚拟设备API 22会显示但不会执行任何操作。那么问题是什么?
public class MapsActivity extends FragmentActivity implements $ b $ OnMapReadyCallback {私人GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
//获取SupportMapFragment并在地图准备好使用时得到通知。
SupportMapFragment mapFragment =(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map){
mMap = map;
LatLng sydney =新LatLng(-34,151);
mMap.addMarker(new MarkerOptions()。position(sydney).title(Marker in Sydney));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
如果(ContextCompat.checkSelfPermission(此,Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED){
mMap.setMyLocationEnabled(真);
} else {
//显示基本原理和请求权限。
}
}
}
在Manifest文件中:
<使用权限android:name =android.permission.INTERNET/> ;
< uses-permission android:name =android.permission.ACCESS_FINE_LOCATION/>
< uses-permission android:name =android.permission.ACCESS_COARSE_LOCATION/>
在此先感谢..
if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED){
mMap.setMyLocationEnabled(true);
} else {
ActivityCompat.requestPermissions(MapActivity.this,
new String [] {Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_CODE);
}
然后您可以通过覆盖此方法来处理结果。
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions [],int [] grantResults){
开关(requestCode){
情况下REQUEST_CODE:{如果
(grantResults.length大于0
&安培;&安培; grantResults [0] == PackageManager.PERMISSION_GRANTED){
/ /做某事
} else {
finish();
}
break;
}
}
}
模拟器的设置为您的应用程序。
尝试后让我知道。祝你好运。
编辑:
您需要实现一个clicklistener到mylocation按钮。
最终GoogleMap mMap = map;
map.setOnMyLocationButtonClickListener(新GoogleMap.OnMyLocationButtonClickListener(){
@覆盖
公共布尔onMyLocationButtonClick(){
经纬度LOC =新经纬度(mMap.getMyLocation()。getLatitude() ,mMap.getMyLocation()getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLng(LOC));
返回真;
}
});
I'm trying to get my current location with the location button on Google Maps but I have some problems. With the new runtime permissions (API level 23), I don't know if i am doing well or not.With that code below, when I use a virtual device API 23, the location button doesn't appear and with a virtual device API 22, it appears but don't do anything. So what can be the problem ?
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{private GoogleMap mMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); // 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); } @Override public void onMapReady(GoogleMap map) { mMap = map; LatLng sydney = new LatLng(-34, 151); mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { mMap.setMyLocationEnabled(true); } else { // Show rationale and request permission. } }
}
In the Manifest file :
<uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Thanks in advance..
In api level 23, you need check permissions programmatically. You are checking the location permission and if it's granted, setting the location enabled. You can ask for permission if it is not granted.
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
} else {
ActivityCompat.requestPermissions(MapActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_CODE);
}
Then you can handle the result by overriding this method.
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//do something
} else {
finish();
}
break;
}
}
}
Also you can give permission from the emulator's settings for your application.
Let me know after trying. Good luck.
EDIT:You need to implement a clicklistener to mylocation button.
final GoogleMap mMap = map;
map.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
@Override
public boolean onMyLocationButtonClick() {
LatLng loc = new LatLng(mMap.getMyLocation().getLatitude(),mMap.getMyLocation().getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLng(loc));
return true;
}
});
这篇关于位置按钮不起作用GoogleMaps v2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!