Android 我没有收到我所在位置的地址(java.lang.IndexOutOfBoundsException:索引:0大小:0)
我有一个代码,必须返回我的设备的坐标和地址以及我在地图上选择的位置,应用程序给我设备的四舍五入坐标,但它不返回地址,而是返回给我的是一个错误: java.lang.IndexOutOfBoundsException:索引:0大小:0 但是当在地图上选择一个地方时,它会返回完整的坐标和地址 代码如下:Android 我没有收到我所在位置的地址(java.lang.IndexOutOfBoundsException:索引:0大小:0),android,geolocation,maps,indexoutofboundsexception,Android,Geolocation,Maps,Indexoutofboundsexception,我有一个代码,必须返回我的设备的坐标和地址以及我在地图上选择的位置,应用程序给我设备的四舍五入坐标,但它不返回地址,而是返回给我的是一个错误: java.lang.IndexOutOfBoundsException:索引:0大小:0 但是当在地图上选择一个地方时,它会返回完整的坐标和地址 代码如下: public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle s
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.camera_fragment, container, false);
myplacebtn = (Button) view.findViewById(R.id.myplacebtn);//Button --get my location
myplacebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (validatePermissionsLocation()) {
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(getActivity(),
android.Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
return;
}
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
showCurrentLocation();
}
}
});
//create the map
mapView = (MapView) view.findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
//get a MapView
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
//Crea marker when i select a place in the map
// Setting a click event handler for the map
map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
//Make the Address
getAddress(latLng);
}
});
map.getUiSettings().setMyLocationButtonEnabled(false);
LatLng jerusalem = new LatLng(32.1105435, 34.8683683);
CameraUpdate miLocation = CameraUpdateFactory.newLatLngZoom(jerusalem, 11);
map.moveCamera(CameraUpdateFactory.newLatLng(jerusalem));
googleMap.animateCamera(miLocation);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(jerusalem);
if (ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(),
android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(),
android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
}
});
return view;
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
}
public void onStatusChanged(String s, int i, Bundle b) {
}
public void onProviderDisabled(String s) {
// gpsDialog();
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
public void onProviderEnabled(String s) {
}
}
//Location of my device
protected void showCurrentLocation() {
if (ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(),
android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(),
android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
//setCoordinates.setText(location.getLatitude() + " , " +
location.getLongitude());
LatLng latLng=new
LatLng(location.getLatitude(),location.getLongitude());
//Make Address
getAddress(latLng);
CameraUpdate miLocation = CameraUpdateFactory.newLatLngZoom(latLng,11);
map.animateCamera(miLocation);
}
}
//Method return the coordinates and the address
private void getAddress(LatLng latLng){
Geocoder geocoder;
List<android.location.Address> direccion = null;
geocoder = new Geocoder(getActivity(), Locale.getDefault());
// Creating a marker
MarkerOptions markerOptions = new MarkerOptions();
try {
direccion = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1); // 1 representa la cantidad de resultados a obtener
String address = direccion.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = direccion.get(0).getLocality();
// Setting the title for the marker.
// This will be displayed on taping the marker
markerOptions.title(city + " : " + address);
} catch (IOException e) {
Toast.makeText(getActivity(),e.toString(),Toast.LENGTH_LONG).show();
markerOptions.title(latLng.latitude + " , " + latLng.longitude);
}
catch (Exception e){
Toast.makeText(getActivity(),e.toString(),Toast.LENGTH_LONG).show();
markerOptions.title(latLng.latitude + " , " + latLng.longitude);
}
// Setting the position for the marker
markerOptions.position(latLng);
setCoordinates.setText(latLng.latitude + " , " + latLng.longitude);
latitude = latLng.latitude;
longitude = latLng.longitude;
// Clears the previously touched position
map.clear();
// Animating to the touched position
map.animateCamera(CameraUpdateFactory.newLatLng(latLng));
// Placing a marker on the touched position
map.addMarker(markerOptions);
}
public View onCreateView(LayoutInflater充气机、@Nullable ViewGroup容器、@Nullable Bundle savedInstanceState){
视图=充气机。充气(R.layout.camera_碎片,容器,假);
myplacebtn=(按钮)view.findViewById(R.id.myplacebtn);//按钮--获取我的位置
myplacebtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(validatePermissionsLocation()){
locationManager=(locationManager)getActivity().getSystemService(Context.LOCATION\u服务);
if(ActivityCompat.checkSelfPermission(getActivity(),android.Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予
&&ActivityCompat.checkSelfPermission(getActivity(),
android.Manifest.permission.ACCESS\u\u位置)
!=PackageManager.权限(已授予){
考虑到呼叫
返回;
}
locationManager.RequestLocationUpdate(
LocationManager.GPS\u提供程序,
两次更新之间的最短时间,
用于更新的最小距离更改,
新建MyLocationListener()
);
showCurrentLocation();
}
}
});
//创建地图
mapView=(mapView)view.findViewById(R.id.mapView);
onCreate(savedInstanceState);
//获取地图视图
getMapAsync(新的OnMapReadyCallback(){
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
地图=谷歌地图;
//在地图中选择一个位置时创建标记
//为映射设置单击事件处理程序
setOnMapClickListener(新的GoogleMap.OnMapClickListener(){
@凌驾
公共无效的点击(LatLng LatLng){
//写地址
获取地址(latLng);
}
});
map.getUiSettings().setMyLocationButtonEnabled(false);
LatLng耶路撒冷=新LatLng(32.1105435,34.8683683);
CameraUpdate miLocation=CameraUpdateFactory.newLatLngZoom(耶路撒冷,11);
移动摄像机地图(CameraUpdateFactory.newLatLng(耶路撒冷));
谷歌地图。动画世界(miLocation);
MarkerOptions MarkerOptions=新MarkerOptions();
markerOptions.位置(耶路撒冷);
如果(ActivityCompat.checkSelfPermission)(getActivity().getApplicationContext()),
android.Manifest.permission.ACCESS\u FINE\u位置)
!=PackageManager.PERMISSION\u已授予和&ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(),
android.Manifest.permission.ACCESS\u\u位置)!=PackageManager.permission\u已授予){
返回;
}
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
}
});
返回视图;
}
私有类MyLocationListener实现LocationListener{
已更改位置上的公共无效(位置){
}
状态已更改的公共void(字符串s、整数i、束b){
}
公共无效onProviderDisabled(字符串s){
//gpsDialog();
startActivity(新意图(设置、动作、位置、来源、设置));
}
已提供已启用的公共void(字符串s){
}
}
//我的设备的位置
受保护的void showCurrentLocation(){
如果(ActivityCompat.checkSelfPermission)(getActivity().getApplicationContext()),
android.Manifest.permission.ACCESS\u FINE\u位置)
!=PackageManager.PERMISSION\u已授予和&ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(),
android.Manifest.permission.ACCESS\u\u位置)!=PackageManager.permission\u已授予){
返回;
}
Location Location=locationManager.getLastKnownLocation(locationManager.GPS\U提供程序);
如果(位置!=null){
//setCoordinates.setText(location.getLatitude()+“,”+
location.getLongitude());
LatLng LatLng=新
LatLng(location.getLatitude(),location.getLongitude());
//地址
获取地址(latLng);
CameraUpdate miLocation=CameraUpdateFactory.newLatLngZoom(latLng,11);
地图。动画片(米洛桑);
}
}
//方法返回坐标和地址
私有地址(LatLng LatLng){
地理编码器;
List direccion=null;
地理编码器=新的地理编码器(getActivity(),Locale.getDefault());
//创建标记
MarkerOptions MarkerOptions=新MarkerOptions();
试一试{
direccion=geocoder.getFromLocation(latLng.latitude,latLng.longitude,1);//1代表一个获取者的结果
String address=direccion.get(0).getAddressLine(0);//如果存在除此之外的任何其他地址行,请通过getMaxAddressLineIndex()检查最大可用地址行
字符串city=direccion.get(0.getLocality();
//设置标记的标题。
//这将显示在磁带上
if (direccion!= null && !direccion.isEmpty()){
String address = direccion.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = direccion.get(0).getLocality();
// Setting the title for the marker.
// This will be displayed on taping the marker
markerOptions.title(city + " : " + address);
}
//Method return the coordinates and the address
private void getAddress(LatLng latLng){
Geocoder geocoder;
List<android.location.Address> direccion = null;
geocoder = new Geocoder(getActivity(), Locale.getDefault());
// Creating a marker
MarkerOptions markerOptions = new MarkerOptions();
try {
direccion = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1); // 1 representa la cantidad de resultados a obtener
String address = direccion.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = direccion.get(0).getLocality();
// Setting the title for the marker.
// This will be displayed on taping the marker
markerOptions.title(city + " : " + address);
} catch (IOException e) {
Toast.makeText(getActivity(),e.toString(),Toast.LENGTH_LONG).show();
markerOptions.title(latLng.latitude + " , " + latLng.longitude);
}
catch (Exception e){
Toast.makeText(getActivity(),e.toString(),Toast.LENGTH_LONG).show();
markerOptions.title(latLng.latitude + " , " + latLng.longitude);
}
// Setting the position for the marker
markerOptions.position(latLng);
setCoordinates.setText(latLng.latitude + " , " + latLng.longitude);
latitude = latLng.latitude;
longitude = latLng.longitude;
// Clears the previously touched position
map.clear();
// Animating to the touched position
map.animateCamera(CameraUpdateFactory.newLatLng(latLng));
// Placing a marker on the touched position
map.addMarker(markerOptions);
}
try {
direccion = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1); // 1 representa la cantidad de resultados a obtener
String address = direccion.get(0).getAddressLine(0); // crash happens here
String city = direccion.get(0).getLocality();
// Setting the title for the marker.
// This will be displayed on taping the marker
markerOptions.title(city + " : " + address);
}