Android 未调用GoogleMap.setOnMapLoadedCallback
我想使用谷歌地图和地理编码功能获取位置。找到我的正确纬度,经度,格式化地址。调用了onMapReady函数,但根本不调用onmappload函数 舱单:Android 未调用GoogleMap.setOnMapLoadedCallback,android,dictionary,location,android-mapview,Android,Dictionary,Location,Android Mapview,我想使用谷歌地图和地理编码功能获取位置。找到我的正确纬度,经度,格式化地址。调用了onMapReady函数,但根本不调用onmappload函数 舱单: <meta-data android:name="com.google.android.geo.API_KEY" android:value="..." /> 我的customMapView布局: <?xml version="1.0" encoding="utf-8"?> &l
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="..." />
我的customMapView布局:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<com.google.android.gms.maps.MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/address_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:ellipsize="end"
android:maxLines="1"
android:padding="5dp"
android:singleLine="true"
android:visibility="visible"
tools:text="21 Jump St, Atlanta GA 30311" />
</merge>
试试这段代码,您还并没有调用MapView的正确生命周期方法。这已经在报告中解释过了 您可以自定义视图: 你的活动
有onStart,onStop。。。还有更多的MapView方法,您需要从活动中手动调用这些方法才能让map完全运行。在创建后不要调用MapView.onResume,请在活动生命周期的正确位置执行。请查看我的更新问题。我仍然看不到您在正确位置调用MapView.onStart和MapView.onResume。必须在与活动相同的生命周期中调用它。在这里看到更多。在这里,您将MapView放在自定义LinearLayout中,因此必须在此自定义视图中创建所有生命周期方法,并将调用传递给MapView。在这里调用onCreatenull和onResume的方式并不是它假定的工作方式。是否显示了映射和标记?我会在我这边检查,然后通知你是的,地图上显示了我的代码和你的代码。但我的问题是,没有调用setOnMapLoadedCallback函数。我遇到了很多麻烦,请帮助我。文档中说:如果由于连接问题导致地图从未加载,或者由于用户不断与地图交互导致地图不断更改且从未完成加载,则不会触发此事件。。那么,你的连接是否良好,你在等待事件时是否在其上做了任何手势?我在等待它,但根本没有触摸它,我在Wific上的连接在控制台中创建api密钥,使用我的应用程序的包名和SHA-1证书指纹限制它,启用Google Map SDK api并完成。检查您的Logcat,如果密钥无效,它将显示一些红线错误
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<com.google.android.gms.maps.MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/address_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:ellipsize="end"
android:maxLines="1"
android:padding="5dp"
android:singleLine="true"
android:visibility="visible"
tools:text="21 Jump St, Atlanta GA 30311" />
</merge>
public class CustomMapView extends LinearLayout {
private MapView mapView;
private TextView textView;
public CustomMapView(Context context) {
this(context, null);
}
public CustomMapView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(context);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public CustomMapView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize(context);
}
private void initialize(Context context) {
setOrientation(LinearLayout.VERTICAL);
LayoutInflater.from(context).inflate(R.layout.map_view, this, true);
this.mapView = ViewUtil.findById(this, R.id.map_view);
this.textView = ViewUtil.findById(this, R.id.address_view);
}
public void display(Place place) {
this.mapView.onCreate(null);
this.mapView.onResume();
this.mapView.setVisibility(View.VISIBLE);
this.imageView.setVisibility(View.GONE);
this.mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(final @Nonnull GoogleMap googleMap) {
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLong(), 13));
googleMap.addMarker(new MarkerOptions().position(place.getLatLong()));
googleMap.setBuildingsEnabled(true);
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.getUiSettings().setAllGesturesEnabled(false);
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLong(), googleMap.getMaxZoomLevel() - 4));
googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
googleMap.snapshot(new GoogleMap.SnapshotReadyCallback() {
@Override
public void onSnapshotReady(Bitmap bitmap) {
// it is not called
}
});
}
});
}
});
this.textView.setText(place.getDescription());
}
}
public class CustomMapView extends LinearLayout {
private MapView mapView;
private TextView textView;
public CustomMapView(Context context) {
this(context, null);
}
public CustomMapView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(context);
}
public void onCreate(Bundle var1) {
mapView.onCreate(var1);
}
public void onResume() {
mapView.onResume();
}
public void onPause() {
mapView.onPause();
}
public void onStart() {
mapView.onStart();
}
public void onStop() {
mapView.onStop();
}
public void onDestroy() {
mapView.onDestroy();
}
public void onLowMemory() {
mapView.onLowMemory();
}
public void onSaveInstanceState(Bundle var1) {
mapView.onSaveInstanceState(var1);
}
private void initialize(Context context) {
setOrientation(LinearLayout.VERTICAL);
LayoutInflater.from(context).inflate(R.layout.map_view, this, true);
this.mapView = ViewUtil.findById(this, R.id.map_view);
this.textView = ViewUtil.findById(this, R.id.address_view);
}
public void display(Place place) {
this.mapView.setVisibility(View.VISIBLE);
this.imageView.setVisibility(View.GONE);
this.mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(final @Nonnull GoogleMap googleMap) {
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLong(), 13));
googleMap.addMarker(new MarkerOptions().position(place.getLatLong()));
googleMap.setBuildingsEnabled(true);
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.getUiSettings().setAllGesturesEnabled(false);
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLong(), googleMap.getMaxZoomLevel() - 4));
googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
googleMap.snapshot(new GoogleMap.SnapshotReadyCallback() {
@Override
public void onSnapshotReady(Bitmap bitmap) {
// it is not called
}
});
}
});
}
});
this.textView.setText(place.getDescription());
}
public class MainActivity extends Activity {
private CustomMapView customView;
@Override
public void onCreate(Bundle var1) {
super.onCreate(var1);
customView = ViewUtil.findById(this, R.id.custom_view_id);
customView.onCreate(var1);
}
@Override
public void onResume() {
super.onResume();
customView.onResume();
}
@Override
public void onPause() {
super.onPause();
customView.onPause();
}
@Override
public void onStart() {
super.onStart();
customView.onStart();
}
@Override
public void onStop() {
super.onStop();
customView.onStop();
}
@Override
public void onDestroy() {
super.onDestroy();
customView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
customView.onLowMemory();
}
@Override
public void onSaveInstanceState(Bundle var1) {
super.onSaveInstanceState(var1);
customView.onSaveInstanceState(var1);
}
}