android studio onMapReady 未调用 [英] android studio onMapReady not called
问题描述
我想在我的一个视图中集成地图视图.
I want to integrate a map view in one of my views.
我生成了一个新的地图片段.它出现在不同的视图中,就像一个魅力.
I have generated a new map fragment. It comes up in a different view and works like a charm.
然后我尝试将代码集成到一个正常的活动中(一个带有操作栏等的活动).它有点工作 - 出现在屏幕上很好,但在那种环境中永远不会调用 onMapReady(换句话说,我被困在非洲,而不是去悉尼).
I then tried to integrate the code in a normal activity (one with actionbar and so forth). It kinda works - comes up on the screen fine, but onMapReady never gets called in that environment (in other words, I stay stuck in Africa, instead of going to Sydney).
坚持了几个小时...
这里是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));
}
}
}
有什么建议吗?
推荐答案
我得到了答案 - 有点.
I got the answer - sort of.
关于片段活动的整个代码永远不会被调用.我尝试了几种方案来激活这个片段,但都无疾而终...
The whole code concerning the fragment activity never gets called. I tried several solutions to activate this fragment, but all came to a dead end...
另一种解决方案是继续使用主活动(AppCompatActivity 的子类).原来AppCompatActivity可以直接实现一个OnMapReadyCallback.
An alternate solution is to continue using the main activity (a subclass of AppCompatActivity). It turns out that AppCompatActivity can directly implement a OnMapReadyCallback.
所以我加了实现OnMapReadyCallback",然后直接加了一个onMapReady"回调.代码如下所示:
So I added "implement OnMapReadyCallback", and then added directly a "onMapReady" callback. Code looks like this :
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));
}
工作正常.我得到了一个完整的活动(工具栏、菜单处于活动状态,还有一个额外的浮动按钮),并且底层地图工作正常......
Works fine. I get a full activity (with the toolbar, menus active, and an additional floating button), and the underlying map works fine...
这篇关于android studio onMapReady 未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!