android - 如何在 Fragment Here Map API 中使用 MapFragment
问题描述
我的应用程序有 4 个用于使用ViewPager的选项卡,其中一个我想显示HERE MAPS。我能够让地图在 Activity 中完美运行,但是在 Fragment 类中,当我尝试将 Fragment 视图投射到 MapFragment 时会引发错误。
这是我的 XML 代码示例
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@android:color/transparent">
<!-- Map Fragment embedded with the map object -->
<fragment
android:id="@+id/mapfragment"
class="com.here.android.mpa.mapping.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
在我的片段代码示例下方
public class CurrentLocationFragment extends Fragment {
// map embedded in the map fragment
private MapFragment mapFragment = null;
private Map map = null;
private static MapRoute mapRoute1;
private String locationAddress = "";
private Double latitude = 18.496252;
private Double langitude = 73.802118;
private static View view;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.fragment_current_location, container, false);
} catch (InflateException e) {
e.printStackTrace();
}
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initialize();
//displayMap();
}
private void initialize() {
// Search for the map fragment to finish setup by calling init().
mapFragment = (MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.mapfragment);
mapFragment.init(new OnEngineInitListener() {
@Override
public void onEngineInitializationCompleted(OnEngineInitListener.Error error) {
if (error == OnEngineInitListener.Error.NONE) {
map = null;
map = mapFragment.getMap();
// Set the map center to the Vancouver region (no animation)
map.setCenter(new GeoCoordinate(latitude, langitude, 0.0),
Map.Animation.NONE);
map.getPositionIndicator().setVisible(true);
try {
Image img_current_location = new Image();
img_current_location.setImageResource(R.drawable.marker);
map.getPositionIndicator().setMarker(img_current_location);
} catch (IOException e) {
e.printStackTrace();
}
} else {
Log.e("HEREMAP", "Cannot initialize MapFragment (" + error + ")");
}
}
});
}
@Override
public void onDestroyView() {
super.onDestroyView();
try{
if (mapFragment != null)
getActivity().getFragmentManager().beginTransaction().remove(mapFragment).commit();
}catch (Exception e){
e.printStackTrace();
}
}
}
当我们重新访问 Map 时,创建 Map 的 InflateException 如下所示。
W/System.err: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
Caused by: java.lang.IllegalArgumentException: Binary XML file line #9: Duplicate id 0x7f0a010b, tag null, or parent id 0xffffffff with another fragment for com.here.android.mpa.mapping.MapFragment
如何在 Fragment中使用HERE 地图。
先感谢您!!
解决方案
马特建议的答案有效,但它会导致重新创建和重新绘制地图,这并不总是可取的。经过大量的试验和错误,我找到了一个适合我的解决方案:
private static View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.map, container, false);
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
return view;
}
为了更好地衡量,这里是带有 R.id.mapFragment (android:id="@+id/mapFragment") 的 "map.xml" (R.layout.map):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
推荐阅读
- ms-access - 托管在远程服务器上的 Microsoft Access accdb 的用户列表
- php - Facebook 抓取 APU $accessToken = $helper->getAccessToken() 没有价值
- python-3.x - Pyzmail/IMAPclient:不知道使用什么键
- javascript - 使用 Sequelize 按相关关键字查找所有图像,其中包含要包含/排除的关键字 ID 值数组
- javascript - 当只有一个选项时,Angular 会自动选择 Mat-Select
- ios - xcode警告“在ios 11之前估计部分页脚高度”是什么意思?
- c++ - 习惯于在c++中读取文件时忽略函数异常
- python - 运行并发 pysftp 命令的建议
- here-api - HERE 交通事故 API 调用不显示事故
- node.js - 如何通过`npm`搜索范围包名称?