Java 谷歌地图赢得';t load-Android
我得到的当前错误是:使用newLatLngBounds(LatLngBounds,int)时出错:映射大小不能为0。很可能地图视图尚未出现布局。等待布局完成,或者使用新的LatLngBounds(LatLngBounds,int,int,int)来指定地图的尺寸。 我假设我得到这个错误是因为布局还没有加载,我如何在代码中纠正这个问题Java 谷歌地图赢得';t load-Android,java,android,google-maps,Java,Android,Google Maps,我得到的当前错误是:使用newLatLngBounds(LatLngBounds,int)时出错:映射大小不能为0。很可能地图视图尚未出现布局。等待布局完成,或者使用新的LatLngBounds(LatLngBounds,int,int,int)来指定地图的尺寸。 我假设我得到这个错误是因为布局还没有加载,我如何在代码中纠正这个问题 import android.os.Bundle; import android.support.v4.app.FragmentActivity; imp
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
private LatLngBounds AUSTRALIA = new LatLngBounds(
new LatLng(-44, 113), new LatLng(-10, 154));
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
/**
* Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
* installed) and the map has not already been instantiated.. This will ensure that we only ever
* call {@link #setUpMap()} once when {@link #mMap} is not null.
* <p/>
* If it isn't installed {@link SupportMapFragment} (and
* {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
* install/update the Google Play services APK on their device.
* <p/>
* A user can return to this FragmentActivity after following the prompt and correctly
* installing/updating/enabling the Google Play services. Since the FragmentActivity may not
* have been completely destroyed during this process (it is likely that it would only be
* stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this
* method in {@link #onResume()} to guarantee that it will be called.
*/
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
/**
* This is where we can add markers or lines, add listeners or move the camera. In this case, we
* just add a marker near Africa.
* <p/>
* This should only be called once and when we are sure that {@link #mMap} is not null.
*/
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(AUSTRALIA, 0));
}
}
导入android.os.Bundle;
导入android.support.v4.app.FragmentActivity;
导入com.google.android.gms.maps.CameraUpdateFactory;
导入com.google.android.gms.maps.GoogleMap;
导入com.google.android.gms.maps.SupportMapFragment;
导入com.google.android.gms.maps.model.LatLng;
导入com.google.android.gms.maps.model.LatLngBounds;
导入com.google.android.gms.maps.model.MarkerOptions;
公共类映射活动扩展了FragmentActivity{
私有GoogleMap mMap;//如果Google Play services APK不可用,则可能为空。
澳大利亚私人LatLngBounds=新LatLngBounds(
新车床(-44113),新车床(-10154));
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
setupmapifneed();
}
@凌驾
受保护的void onResume(){
super.onResume();
setupmapifneed();
}
/**
*如果可能的话,设置地图(例如,Google Play services APK已正确启动)
*已安装)并且映射尚未实例化。这将确保
*当{@link#mMap}不为null时,调用{@link#setUpMap()}一次。
*
*如果未安装{@link SupportMapFragment}(和
*{@link com.google.android.gms.maps.MapView MapView})将提示用户
*在其设备上安装/更新Google Play services APK。
*
*用户可以按照提示正确返回此FragmentActivity
*安装/更新/启用Google Play服务。因为碎片活动可能不会
*在这一过程中被完全破坏(很可能
*停止或暂停),{@link#onCreate(Bundle)}可能不会被再次调用,因此我们应该调用它
*方法来保证将调用它。
*/
私有void setUpMapIfNeeded(){
//执行空检查以确认尚未实例化映射。
如果(mMap==null){
//尝试从SupportMapFragment获取映射。
mMap=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
//检查我们是否成功获得地图。
如果(mMap!=null){
setUpMap();
}
}
}
/**
*在这里,我们可以添加标记或线条、添加侦听器或移动摄影机
*在非洲附近加一个标记。
*
*当我们确定{@link#mMap}不是null时,应该只调用一次。
*/
私有void setUpMap(){
mMap.addMarker(新的MarkerOptions().position(新的LatLng(0,0)).title(“Marker”);
mMap.setMapType(GoogleMap.MAP\u TYPE\u HYBRID);
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(澳大利亚,0));
}
}
行:
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(澳大利亚,0))SetUpMap()中的code>代码>方法。顺便问一下,是什么导致了这个问题,当我试图使用google的API V2文档时,我是否需要在地图加载后对google地图使用回调,或者在特定时间段后使用单独的方法进行初始化 看起来您的地图还没有准备好。
您可以使用MapLoadedCallBack
map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 30));
}
});
你也可以先告诉我一件事
map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition arg0) {
map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 30));
}
});
请检查以下问题:[使用CameraUpdateFactory.newLatLngBounds崩溃的移动摄像头][1][1]:好的,那么我将在活动中的何处更改MapLoadedCallBack?我试图实现这两种解决方案,但我不断地出现奇怪的错误。例如,方法上面的map.SetOnMapLoadedCallback在哪里?或者它会停留在onmapload方法上方的同一点上吗?我会停留在同一点上,它只会在这个映射完成渲染时调用回调。