Here Maps - 在 TabLayout 的片段中使用 MapFragment
Here Maps - use MapFragment in a Fragment of TabLayout
我的应用程序有 3 个选项卡,我想在其中一个选项卡中显示一张地图(HERE MAPS,而不是 Google 地图)。我能够让地图在 Activity 中完美运行,但在 Fragment Class 中,当我尝试将 Fragment 视图转换为 MapFragment 时会抛出错误。
MapFragment mapFragment = (MapFragment)
getFragmentManager().findFragmentById(R.id.mapfragment);
错误:
不可转换类型:无法将 android.support.v4.app.support 转换为 com.here.android.mpa.mapping.MapFragment
如果我错了请纠正我,但发生这种情况的原因是因为我们不能在 android.app.Fragment(用于显示地图,如 HERE MAPS DOC 中所述) =39=].v4.app.Fragment(用于 TabsLayout)。
我使用 Google 地图发现了很多与此错误相关的问题。但是在使用 HERE MAPS 和 none 时只有两个(first,second)关于相同的错误确实有助于解决问题。
在 google 地图中,您可以使用 SupportMapFragment(),但此方法仅适用于 Google 地图。使用 Here Maps 有什么解决方案吗?也许用不同的方法来达到相同的目标?还是我在 TabLayout 中实现此 Here Maps 时遗漏了什么?
任何帮助将不胜感激!
我的代码:
MapFragment.java
public class Map extends Fragment {
public Map() {}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
MapFragment mapFragment = (MapFragment)
getFragmentManager().findFragmentById(R.id.mapfragment);
mapFragment.init(new OnEngineInitListener() {
@Override
public void onEngineInitializationCompleted(
OnEngineInitListener.Error error) {
if (error == OnEngineInitListener.Error.NONE) {
com.here.android.mpa.mapping.Map map = mapFragment.getMap();
} else {
System.out.println("ERROR: Cannot initialize MapFragment");
}
}
};
return view;
}
fragment_map.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.modulos.tabsMenu.Config">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/mapFragmentContainer"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center"
android:background="#aaa" >
<fragment
class="com.here.android.mpa.mapping.MapFragment"
android:id="@+id/mapfragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center"
android:background="#aaa" >
<!-- other things here -->
</LinearLayout>
</FrameLayout>
问这个肯定是愚蠢的,但你检查过你是否使用了正确包中的 MapFragment 吗?
你一定是在使用这个classcom.here.android.mpa.mapping.MapFragment,可能是你导入这个com.google.android.gms.maps.MapFragment.
由于 HERE Maps SDK 仅适用于 API 级别 15 及以上,因此不支持兼容性片段(当您瞄准较低的 API 级别时,您主要需要)。
在某些情况下,即使您将 Android 4 或更高版本作为目标,您仍然希望处理支持片段,在这些情况下,您必须使用 HERE SDK 的 MapView 并将其嵌入到您自己的布局中。
请参阅此代码示例,其中还使用了 MapView:https://tcs.ext.here.com/sdk_examples/MapDownloader.zip
所以,它看起来像这样:
在您的布局中添加 MapView:
<com.here.android.mpa.mapping.MapView
android:id="@+id/ext_mapview"
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="match_parent" />
然后在您的代码中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mapView = (MapView) findViewById(R.id.ext_mapview);
MapEngine.getInstance().init(this, engineInitHandler);
}
private OnEngineInitListener engineInitHandler = new OnEngineInitListener() {
@Override
public void onEngineInitializationCompleted(Error error) {
if (error == Error.NONE) {
map = new Map();
mapView.setMap(map);
// more map initial settings
} else {
Log.e(TAG, "ERROR: Cannot initialize MapEngine " + error);
}
}
};
@Override
public void onResume() {
super.onResume();
MapEngine.getInstance().onResume();
if (mapView != null) {
mapView.onResume();
}
}
@Override
public void onPause() {
if (mapView != null) {
mapView.onPause();
}
MapEngine.getInstance().onPause();
super.onPause();
}
处理 MapEngine 和 MapView 的暂停和恢复很重要,否则性能会很差。
我的应用程序有 3 个选项卡,我想在其中一个选项卡中显示一张地图(HERE MAPS,而不是 Google 地图)。我能够让地图在 Activity 中完美运行,但在 Fragment Class 中,当我尝试将 Fragment 视图转换为 MapFragment 时会抛出错误。
MapFragment mapFragment = (MapFragment)
getFragmentManager().findFragmentById(R.id.mapfragment);
错误: 不可转换类型:无法将 android.support.v4.app.support 转换为 com.here.android.mpa.mapping.MapFragment
如果我错了请纠正我,但发生这种情况的原因是因为我们不能在 android.app.Fragment(用于显示地图,如 HERE MAPS DOC 中所述) =39=].v4.app.Fragment(用于 TabsLayout)。
我使用 Google 地图发现了很多与此错误相关的问题。但是在使用 HERE MAPS 和 none 时只有两个(first,second)关于相同的错误确实有助于解决问题。
在 google 地图中,您可以使用 SupportMapFragment(),但此方法仅适用于 Google 地图。使用 Here Maps 有什么解决方案吗?也许用不同的方法来达到相同的目标?还是我在 TabLayout 中实现此 Here Maps 时遗漏了什么?
任何帮助将不胜感激!
我的代码:
MapFragment.java
public class Map extends Fragment {
public Map() {}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
MapFragment mapFragment = (MapFragment)
getFragmentManager().findFragmentById(R.id.mapfragment);
mapFragment.init(new OnEngineInitListener() {
@Override
public void onEngineInitializationCompleted(
OnEngineInitListener.Error error) {
if (error == OnEngineInitListener.Error.NONE) {
com.here.android.mpa.mapping.Map map = mapFragment.getMap();
} else {
System.out.println("ERROR: Cannot initialize MapFragment");
}
}
};
return view;
}
fragment_map.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.modulos.tabsMenu.Config">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/mapFragmentContainer"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center"
android:background="#aaa" >
<fragment
class="com.here.android.mpa.mapping.MapFragment"
android:id="@+id/mapfragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center"
android:background="#aaa" >
<!-- other things here -->
</LinearLayout>
</FrameLayout>
问这个肯定是愚蠢的,但你检查过你是否使用了正确包中的 MapFragment 吗?
你一定是在使用这个classcom.here.android.mpa.mapping.MapFragment,可能是你导入这个com.google.android.gms.maps.MapFragment.
由于 HERE Maps SDK 仅适用于 API 级别 15 及以上,因此不支持兼容性片段(当您瞄准较低的 API 级别时,您主要需要)。 在某些情况下,即使您将 Android 4 或更高版本作为目标,您仍然希望处理支持片段,在这些情况下,您必须使用 HERE SDK 的 MapView 并将其嵌入到您自己的布局中。 请参阅此代码示例,其中还使用了 MapView:https://tcs.ext.here.com/sdk_examples/MapDownloader.zip
所以,它看起来像这样:
在您的布局中添加 MapView:
<com.here.android.mpa.mapping.MapView
android:id="@+id/ext_mapview"
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="match_parent" />
然后在您的代码中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mapView = (MapView) findViewById(R.id.ext_mapview);
MapEngine.getInstance().init(this, engineInitHandler);
}
private OnEngineInitListener engineInitHandler = new OnEngineInitListener() {
@Override
public void onEngineInitializationCompleted(Error error) {
if (error == Error.NONE) {
map = new Map();
mapView.setMap(map);
// more map initial settings
} else {
Log.e(TAG, "ERROR: Cannot initialize MapEngine " + error);
}
}
};
@Override
public void onResume() {
super.onResume();
MapEngine.getInstance().onResume();
if (mapView != null) {
mapView.onResume();
}
}
@Override
public void onPause() {
if (mapView != null) {
mapView.onPause();
}
MapEngine.getInstance().onPause();
super.onPause();
}
处理 MapEngine 和 MapView 的暂停和恢复很重要,否则性能会很差。