我想在我的一个视图中集成地图视图。
我已经生成了一个新的地图片段。它以不同的视角呈现,并且像魅力一样。
然后我尝试将代码集成到正常活动中(一个带有操作栏等等)。有点工作 - 在屏幕上显示得很好,但onMapReady永远不会在那个环境中调用(换句话说,我会留在非洲,而不是去悉尼)。
已经好几个小时......
这是xml代码:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<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="com.example.jp.artfoundui.MapsActivity" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/monument_capture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_menu_camera" />
这是java代码:
public class MonumentActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_monument);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(MapsActivity.this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
}
有什么建议吗?
答案 0 :(得分:2)
我得到了答案 - 有点。
关于片段活动的整个代码永远不会被调用。我尝试了几种解决方案来激活这个片段,但都走到了死胡同......
另一种解决方案是继续使用主要活动(AppCompatActivity的子类)。事实证明,AppCompatActivity可以直接实现OnMapReadyCallback。
所以我添加了“实现OnMapReadyCallback”,然后直接添加了“onMapReady”回调。代码如下所示:
public class MonumentActivity extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_monument);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.monument_map);
mapFragment.getMapAsync(this);
[...]
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng louvres = new LatLng(48.860294, 2.338629);
mMap.addMarker(new MarkerOptions().position(louvres).title("Marker sur le Louvres"));
// mMap.moveCamera(CameraUpdateFactory.newLatLng(louvres));
// mMap.moveCamera(CameraUpdateFactory.zoomTo(18));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(louvres, 15));
}
工作正常。我得到一个完整的活动(工具栏,菜单处于活动状态,还有一个额外的浮动按钮),底层地图工作正常...