H. *_*lyn 5 android google-maps android-mapview
我将尝试在我的Android应用程序中显示一个名为的片段的地图RoeteFragment.如果我调试我的代码,我发现onMapReady永远不会调用该方法,因此不会加载地图.
片段实现了OnMapReadyCallback所需的,在onCreateView我得到MapView和调用getMapAsync.如果我在该方法上放置断点,它将始终被击中,内部的断点onMapReady从未被击中.没有异常抛出.
我还在该文件中提供了有效的Google Maps API密钥res/values/google_maps_api.xml.
这是我的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
public class RoeteFragment extends Fragment implements OnMapReadyCallback {
private MapView mapView;
private static Roete _roete;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_roete, container, false);
mapView = (MapView) view.findViewById(R.id.map);
mapView.getMapAsync(this);
return view;
}
@Override
public void onMapReady(GoogleMap googleMap) {
if (_roete != null && _roete.getAankomstLocatie() != null && _roete.getVertrekLocatie() != null) {
LatLng aankomst = new LatLng(_roete.getAankomstLocatie().getLatitude(), _roete.getAankomstLocatie().getLongitude());
googleMap.addMarker(new MarkerOptions().position(aankomst).title("aankomst"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(aankomst));
LatLng vertrek = new LatLng(_roete.getVertrekLocatie().getLatitude(), _roete.getVertrekLocatie().getLongitude());
googleMap.addMarker(new MarkerOptions().position(vertrek).title("vertrek"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(vertrek));
}
}
public static Fragment newInstance() {
return new RoeteFragment ();
}
public static Fragment newInstance(Roete roete) {
_roete = roete;
return newInstance();
}
}
Run Code Online (Sandbox Code Playgroud)
你可以在我的代码中提交错误吗?
小智 7
地图片段是您扩展到另一个片段的布局的一部分(我们称之为父片段)。膨胀的片段成为父片段的活动而非活动的子片段。您必须询问父片段的子片段管理器:
注意:只需扩展简单的片段 public class MapsFragments extends Fragment implements OnMapReadyCallback {}
XML 文件
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="net.elektrasoft.Test.MapsFragments" />
Run Code Online (Sandbox Code Playgroud)
和
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("check","onCreateView");
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_maps_fragments, container, false);
// Gets the MapView from the XML layout and creates it
final SupportMapFragment myMAPF = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.map);
myMAPF.getMapAsync(this);
return view;
}`
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11184 次 |
| 最近记录: |