Android 使用“自动完成”搜索地点后,地图活动崩溃
嗨,我是安卓地图的新成员,我有一个地图活动,当我用自动完成功能搜索地址时会添加标记,当搜索地址(街道、州、国家)时一切正常,问题是当我按麦当劳或特定建筑等地搜索时,自动完成功能正常,但当它应该添加标记时,活动崩溃。我已经有谷歌地图api密钥和谷歌位置服务器密钥 在下面添加标记代码 提前谢谢Android 使用“自动完成”搜索地点后,地图活动崩溃,android,google-maps-api-3,google-places-api,Android,Google Maps Api 3,Google Places Api,嗨,我是安卓地图的新成员,我有一个地图活动,当我用自动完成功能搜索地址时会添加标记,当搜索地址(街道、州、国家)时一切正常,问题是当我按麦当劳或特定建筑等地搜索时,自动完成功能正常,但当它应该添加标记时,活动崩溃。我已经有谷歌地图api密钥和谷歌位置服务器密钥 在下面添加标记代码 提前谢谢 Intent o = getIntent(); String textOrigin = o.getStringExtra("TextBoxOrigin"); List<Address>
Intent o = getIntent();
String textOrigin = o.getStringExtra("TextBoxOrigin");
List<Address> addressListOrigin=null;
if(textOrigin !=null)
{
Geocoder geocoderOrigin=new Geocoder(this);
try {
addressListOrigin=geocoderOrigin.getFromLocationName(textOrigin,1);
}catch (IOException e){
e.printStackTrace();
}
//Check if origin exist
if(addressListOrigin != null && addressListOrigin.size() > 0) {
Address addressOrigin = addressListOrigin.get(0);
LatLng latLng = new LatLng(addressOrigin.getLatitude(), addressOrigin.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title("your position").icon(BitmapDescriptorFactory.fromResource(R.mipmap.worker_pin2)));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
CameraUpdate cameraUpdate1 = CameraUpdateFactory.newLatLngZoom(latLng, 12);
mMap.animateCamera(cameraUpdate1);
}else{
noLocationFound();
}
Intent o=getIntent();
字符串textOrigin=o.getStringExtra(“TextBoxOrigin”);
列表地址ListOrigin=null;
if(textOrigin!=null)
{
Geocoder geocoderOrigin=新的地理编码器(本);
试一试{
addressListOrigin=geocoderOrigin.getFromLocationName(textOrigin,1);
}捕获(IOE异常){
e、 printStackTrace();
}
//检查来源是否存在
if(addressListOrigin!=null&&addressListOrigin.size()>0){
AddressAddressOrigin=addressListOrigin.get(0);
LatLng LatLng=新LatLng(addressOrigin.getLatitude(),addressOrigin.getLongitude());
mMap.addMarker(新的MarkerOptions().position(latLng).title(“您的位置”).icon(BitmapDescriptorFactory.fromResource(R.mipmap.worker_pin2));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
CameraUpdate cameraUpdate1=CameraUpdateFactory.newLatLngZoom(latLng,12);
mMap.animateCamera(cameraUpdate1);
}否则{
noLocationFound();
}
Geocoder的getFromLocationName()方法必须在UI的单独线程上运行,请在此处查看答案:
地理编码器的getFromLocationName()方法必须在UI的单独线程上运行,请在此处查看答案: