无法将java.util.arraylist强制转换为android.location.address
我试图将所有地址(字符串)放入一个列表中,然后逐个获取它们,并用标记填充一个映射,但我遇到了这样一个错误,即java.util.arraylist无法转换为android.location.address。有什么帮助吗 这是生成错误的代码片段无法将java.util.arraylist强制转换为android.location.address,android,geolocation,google-maps-markers,Android,Geolocation,Google Maps Markers,我试图将所有地址(字符串)放入一个列表中,然后逐个获取它们,并用标记填充一个映射,但我遇到了这样一个错误,即java.util.arraylist无法转换为android.location.address。有什么帮助吗 这是生成错误的代码片段 int i = 0; List<List<Address>> addressList = new ArrayList<List<Address>>();
int i = 0;
List<List<Address>> addressList = new ArrayList<List<Address>>();
//while (indirizzi != null) {
while (i <= 3) {
try {
addressList.add(geocoder.getFromLocationName(indirizzi.get(i), 1));
Log.i("indirizzo i-esimo",indirizzi.get(i));
i++;
} catch (IOException e) {
Log.i("geolocation","geolocation IOException");
e.printStackTrace();
}
}
for (int j = 0; j < addressList.size(); j++) {
Address address = (Address) addressList.get(j);
if(address.hasLatitude() && address.hasLongitude()){
latLng = new LatLng(address.getLatitude(), address.getLongitude());
}
markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(indirizzi.get(i));
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.icn_albero));
Log.i("for", Integer.toString(i));
j++;
googleMap.addMarker(markerOptions);
}
inti=0;
List addressList=new ArrayList();
//while(indirizzi!=null){
(i查看定义时,它是的列表
的地址的列表
List<List<Address>> addressList = new ArrayList<List<Address>>();
因为这将给您一个列表
,它不是地址
对象
您可以:
Address address = (Address) addressList.get(j).get(someOtherIndex);
将列表定义为:
List<Address> addressList = new ArrayList<Address>();
List addressList=new ArrayList();
我使用了下面的代码
我已经从oncreate调用了GeocoderTask类
ed.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
addresstext = (String) parent.getItemAtPosition(position);
Toast.makeText(getBaseContext(),""+addresstext+ "", Toast.LENGTH_SHORT).show();
if(addresstext!=null && !addresstext.equals("")){
new GeocoderTask().execute(addresstext);
}
}
});
ed.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父视图、最终视图、,
内部位置,长id){
addresstext=(字符串)parent.getItemAtPosition(位置);
Toast.makeText(getBaseContext(),“”+addresstext+“”,Toast.LENGTH_SHORT).show();
if(addresstext!=null&!addresstext.equals(“”){
新的GeocoderTask().execute(addresstext);
}
}
});
-
私有类GeocoderTask扩展异步任务{
@凌驾
受保护列表doInBackground(字符串…位置名称){
//创建Geocoder类的实例
Geocoder Geocoder=新的Geocoder(getBaseContext());
列表地址=空;
试一试{
//获取最多10个与输入文本匹配的地址
addresses=geocoder.getFromLocationName(locationName[0],10);
System.out.println(“内部后台处理”);
}捕获(IOE异常){
e、 printStackTrace();
}
返回地址;
}
@凌驾
受保护的void onPostExecute(列表地址){
if(addresses==null | | addresses.size()==0){
Toast.makeText(getBaseContext(),“找不到位置”,Toast.LENGTH_SHORT.show();
}
//清除地图上的所有现有标记
如果(标记!=null){
marker.remove();
}
//在谷歌地图上为每个匹配地址添加标记
对于(int i=0;i 0?地址。getAddressLine(0):“”,
address.getCountryName());
System.out.println(“内部OnPostExecuteMathod进程”);
Toast.makeText(getBaseContext(),“”+地址.getLatitude()+“-”+地址.getLatitude()+“”,Toast.LENGTH\u SHORT).show();
CameraPosition CameraPosition=新建CameraPosition.Builder()
.target(latLng)//将地图中心设置为山景
.zoom(17)//设置缩放
.bearing(90)//将相机的方向设置为东
.tilt(30)//将相机的倾斜设置为30度
.build();//从生成器创建CameraPosition
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
地图移动摄像机(CameraUpdateFactory.newLatLngZoom(latLng,18));
marker=map.addMarker(新的MarkerOptions()
.位置(车床)
.title(“+addresstext+”);
marker.showInfoWindow();
}
}
我使用了您向我展示的2方法,但是我必须转换这个地址列表。添加((地址)geocoder.getFromLocationName(indirizzi.get(i),1));它仍然不起作用!使用第一个方法,我得到了一个索引越界错误!
ed.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
addresstext = (String) parent.getItemAtPosition(position);
Toast.makeText(getBaseContext(),""+addresstext+ "", Toast.LENGTH_SHORT).show();
if(addresstext!=null && !addresstext.equals("")){
new GeocoderTask().execute(addresstext);
}
}
});
private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{
@Override
protected List<Address> doInBackground(String... locationName) {
// Creating an instance of Geocoder class
Geocoder geocoder = new Geocoder(getBaseContext());
List<Address> addresses = null;
try {
// Getting a maximum of 10 Address that matches the input text
addresses = geocoder.getFromLocationName(locationName[0], 10);
System.out.println(" Inside Background Process");
} catch (IOException e) {
e.printStackTrace();
}
return addresses;
}
@Override
protected void onPostExecute(List<Address> addresses) {
if(addresses==null || addresses.size()==0){
Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
}
// Clears all the existing markers on the map
if(marker!=null){
marker.remove();
}
// Adding Markers on Google Map for each matching address
for(int i=0;i<addresses.size();i++){
Address address = (Address) addresses.get(i);
// Creating an instance of GeoPoint, to display in Google Map
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
addressText = String.format("%s, %s",
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
address.getCountryName());
System.out.println(" Inside OnPostExecutemeathod Process");
Toast.makeText(getBaseContext(), ""+address.getLatitude()+" - "+address.getLongitude()+"", Toast.LENGTH_SHORT).show();
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(latLng) // Sets the center of the map to Mountain View
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18));
marker = map.addMarker(new MarkerOptions()
.position(latLng)
.title(""+addresstext+""));
marker.showInfoWindow();
}
}