java - 我应该在哪里调用我的更新地图函数?我需要在我的异步任务之后调用它?
问题描述
嗨,我是 Android 编程的新手,我有一个应用程序,当用户点击一个国家/地区时,它会启动另一个活动,显示带有定位首都城市的标记的地图。
但是我需要从我的异步任务方法中获取资金。所以它需要返回国家信息,然后我可以更新我的地图。我在 onPost Execute 中调用了我的更新地图方法,但应用程序只是崩溃了?
在我的创建
mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync( this);
new GetCountryDetails().execute(selectedCountry);
然后在我的异步任务中
private class GetCountryDetails extends AsyncTask<String, String, CountryInfo> {
@Override
protected CountryInfo doInBackground(String... params) {
CountryInfo countryInfo = null;
在我的 onPostExecute
protected void onPostExecute(CountryInfo countryInfo) {
countryCapital=countryInfo.getCapital();
updateMap(map);//doesn't work here
onMapReady:
public void onMapReady(GoogleMap map) {
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.getUiSettings().setMapToolbarEnabled(true);
更新地图
public void updateMap(GoogleMap map){
LatLng capitalCity = getCityLatitude(this, countryCapital);
map.moveCamera(CameraUpdateFactory.newLatLng(capitalCity));
MarkerOptions marker = new MarkerOptions().position(capitalCity).title( countryCapital);
map.addMarker(marker);
}
任何帮助将不胜感激谢谢!
一世
解决方案
Create Map varible as activity field.And define variable in onMapReady function.After that use activity variable as you want. I think it should works.
GoogleMap map;
public void onMapReady(GoogleMap map) {
this.map = map;
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.getUiSettings().setMapToolbarEnabled(true);
}
推荐阅读
- ssh - 谷歌云平台 - 无法通过 SSH 连接到虚拟机
- azure - 如何在复制活动(NoSQL 到 SQL)中过滤 ADF(Azure 数据工厂)V2 中上次成功触发器运行的数据?
- unit-testing - 如何通过 io.mockk 测试使用不同参数多次调用的方法?
- angular - Angular 7 风格不同层次的垫子树
- javascript - 在 Extjs 3.4 中渲染之前格式化加载数据
- .net-core - 如何首先从表数据库更新模型
- c# - 我想获取两个日期之间的记录,但日期列在数据库中是字符串格式。使用 linq 查询
- javascript - How to create an array from object which has specific properties in child objects
- python - 如何从python中找到dll函数?
- javascript - 如何同时处理保存多对多关系?