Android PermissionCheck-around GoogleMap OnMyLocationChangeListener的类型参数错误
我正在开发一个Android应用程序,它应该使用用户在谷歌地图中的位置。但是,LocationManager所需的权限检查在GoogleMap onMyLocationChangeListener方法的上下文中失败,我不知道为什么。在谷歌搜索之后,我还没有找到解决方案,所以任何提示或帮助都会受到热烈欢迎 活动:Android PermissionCheck-around GoogleMap OnMyLocationChangeListener的类型参数错误,android,google-maps,android-studio,google-maps-android-api-2,google-maps-api-2,Android,Google Maps,Android Studio,Google Maps Android Api 2,Google Maps Api 2,我正在开发一个Android应用程序,它应该使用用户在谷歌地图中的位置。但是,LocationManager所需的权限检查在GoogleMap onMyLocationChangeListener方法的上下文中失败,我不知道为什么。在谷歌搜索之后,我还没有找到解决方案,所以任何提示或帮助都会受到热烈欢迎 活动: import android.Manifest; import android.app.Activity; import android.content.Context; import
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import gmbh.webagenten.CustomInfoWindowGoogleMap;
public class GoogleMapInfoWindowActivity extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private static final int EDIT_REQUEST = 1;
private static final int CAMERA_REQUEST = 1888;
static final int REQUEST_IMAGE_CAPTURE = 1;
private ImageView imageView;
Bitmap photo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
private LatLng getCurrentPosition() {
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria(); // set criteria for location provider:
criteria.setAccuracy(Criteria.ACCURACY_FINE); // fine accuracy
criteria.setCostAllowed(false); // no monetary cost
String bestProvider = locationManager.getBestProvider(criteria, false);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
}
Location location = locationManager.getLastKnownLocation(bestProvider);
LatLng myPosition = new LatLng(location.getLatitude(), location.getLongitude());
Toast.makeText(this, "current position: " + location.getLatitude() + "," + location.getLongitude(), Toast.LENGTH_SHORT).show();
return myPosition;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
LatLng myPosition;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng coordinate = new LatLng(latitude, longitude);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 19);
mMap.animateCamera(yourLocation);
}
if (mMap != null) {
// to set current location
googleMap.setMyLocationEnabled(true);
MarkerOptions markerOptions = new MarkerOptions();
/*markerOptions.position(myPosition)
.title("Hier bin ich")
.snippet("Snoqualmie Falls is located 25 miles east of Seattle.")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
*/
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
/*LatLng snowqualmie = new LatLng(53.5621649, 9.9660802);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(snowqualmie)
.title("Hier bin ich")
.snippet("Snoqualmie Falls is located 25 miles east of Seattle.")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
InfoWindowData info = new InfoWindowData();
info.setImage("snowqualmie");
info.setHotel("Hotel : excellent hotels available");
info.setFood("Food : all types of restaurants available");
info.setTransport("Reach the site by bus, car and train.");
CustomInfoWindowGoogleMap customInfoWindow = new CustomInfoWindowGoogleMap(this);
mMap.setInfoWindowAdapter(customInfoWindow);
Marker m = mMap.addMarker(markerOptions);
m.setTag(info);
m.showInfoWindow();
mMap.moveCamera(CameraUpdateFactory.newLatLng(snowqualmie));*/
}
GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager.getBestProvider(new Criteria(), true);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Location locations = locationManager.getLastKnownLocation(provider);
List<String> providerList = locationManager.getAllProviders();
if (null != locations && null != providerList && providerList.size() > 0) {
double longitude = locations.getLongitude();
double latitude = locations.getLatitude();
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> listAddresses = geocoder.getFromLocation(latitude, longitude, 1);
if (null != listAddresses && listAddresses.size() > 0) {
String _Location = listAddresses.get(0).getAddressLine(0);
}
} catch (IOException e) {
e.printStackTrace();
}
}
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp = Bitmap.createBitmap(150, 150, conf);
Canvas canvas1 = new Canvas(bmp);
// paint defines the text color, stroke width and size
Paint color = new Paint();
color.setTextSize(30);
color.setColor(Color.BLACK);
// modify canvas
canvas1.drawBitmap(BitmapFactory.decodeResource(getResources(),
R.drawable.custom_marker), 0, 0, color);
//canvas1.drawText("Your position", 20, 5, color);
// add marker to Map
mMap.addMarker(new MarkerOptions()
.position(loc)
.icon(BitmapDescriptorFactory.fromBitmap(bmp))
// Specifies the anchor to be at a particular point in the marker image.
.anchor(0.5f, 1).title("Your current position").snippet("You are here"));
}
};
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(final LatLng latLng) {
Intent edit = new Intent(GoogleMapInfoWindowActivity.this, EditActivity.class);
edit.putExtra("location", latLng);
GoogleMapInfoWindowActivity.this.startActivityForResult(edit, EDIT_REQUEST);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));
}
};
}
if (requestCode == CAMERA_REQUEST) {
photo = (Bitmap) data.getExtras().get("data");
this.imageView.setImageBitmap(photo);
}
switch (requestCode) {
case (EDIT_REQUEST): {
if (resultCode == Activity.RESULT_OK) {
MarkerOptions markerOptions = data.getParcelableExtra("marker");
mMap.addMarker(markerOptions);
}
break;
}
}
}
}
导入android.Manifest;
导入android.app.Activity;
导入android.content.Context;
导入android.content.Intent;
导入android.content.pm.PackageManager;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.graphics.Paint;
导入android.location.Address;
导入android.location.Criteria;
导入android.location.Geocoder;
导入android.location.location;
导入android.location.LocationListener;
导入android.location.LocationManager;
导入android.os.Bundle;
导入android.support.v4.app.ActivityCompat;
导入android.support.v4.content.ContextCompat;
导入android.support.v7.app.AppActivity;
导入android.widget.ImageView;
导入android.widget.Toast;
导入com.google.android.gms.maps.CameraUpdate;
导入com.google.android.gms.maps.CameraUpdateFactory;
导入com.google.android.gms.maps.GoogleMap;
导入com.google.android.gms.maps.MapFragment;
导入com.google.android.gms.maps.OnMapReadyCallback;
导入com.google.android.gms.maps.SupportMapFragment;
导入com.google.android.gms.maps.model.BitmapDescriptorFactory;
导入com.google.android.gms.maps.model.LatLng;
导入com.google.android.gms.maps.model.Marker;
导入com.google.android.gms.maps.model.MarkerOptions;
导入java.io.IOException;
导入java.util.List;
导入java.util.Locale;
import gmbh.webagenten.CustomInfoWindowGoogleMap;
公共类GoogleMapInfo WindowActivity扩展了AppCompatActivity在MapReadyCallback上的实现{
私有谷歌地图;
私有静态最终整数编辑请求=1;
专用静态最终int摄像机_请求=1888;
静态最终整数请求\图像\捕获=1;
私人影像视图;
位图照片;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
}
私有LatLng getCurrentPosition(){
LocationManager LocationManager=(LocationManager)getSystemService(LOCATION\u服务);
Criteria=new Criteria();//为位置提供程序设置条件:
criteria.setaccurity(criteria.accurity_FINE);//精确
条件。setCostAllowed(false);//无货币成本
字符串bestProvider=locationManager.getBestProvider(条件,false);
if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予和&ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u LOCATION)!=PackageManager.permission\u已授予){
考虑到呼叫
//ActivityCompat#请求权限
//在此处请求缺少的权限,然后覆盖
//public void onRequestPermissionsResult(int-requestCode,字符串[]权限,
//int[]格兰特结果)
//处理用户授予权限的情况。请参阅文档
//对于ActivityCompat,请请求权限以获取更多详细信息。
}
Location Location=locationManager.getLastKnownLocation(bestProvider);
LatLng myPosition=新LatLng(location.getLatitude(),location.getLength());
Toast.makeText(此,“当前位置:”+location.getLatitude()+“,“+location.getLatitude(),Toast.LENGTH_SHORT).show();
返回我的位置;
}
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
mMap=谷歌地图;
mMap.setMapType(GoogleMap.MAP\u TYPE\u HYBRID);
车床位置;
if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予和&ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u LOCATION)!=PackageManager.permission\u已授予){
考虑到呼叫
//ActivityCompat#请求权限
//在此处请求缺少的权限,然后覆盖
//public void onRequestPermissionsResult(int-requestCode,字符串[]权限,
//int[]格兰特结果)
//处理用户授予权限的情况。请参阅文档
//对于ActivityCompat,请请求权限以获取更多详细信息。
返回;
}
mMap.setMyLocationEnabled(真);
LocationManager LocationManager=(LocationManager)getSystemService(LOCATION\u服务);
标准=新标准();
字符串提供程序=locationManager.getBestProvider(条件为true);
Location Location=locationManager.getLastKnownLocation(提供者);
如果(位置!=null){
双纬度=location.getLatitude();
double longitude=location.getLongitude();
LatLng坐标=新LatLng(纬度、经度);
CameraUpdate yourLocation=CameraUpdateFactory.newLatLngZoom(坐标,19);
mMap.animateCamera(您的位置);
}
如果(mMap!=null){
//设置当前位置的步骤
googleMap.setMyLocationEnabled(true);
MarkerOptions MarkerOptions=新MarkerOptions();
/*标记选项位置