Java 当我在google maps api V2上使用moveCamera时,我的应用程序崩溃
我正在使用android的谷歌地图api V2。当我加载地图时,它加载得非常完美 然而,当我试图从 xml:Java 当我在google maps api V2上使用moveCamera时,我的应用程序崩溃,java,android,google-maps,maps,Java,Android,Google Maps,Maps,我正在使用android的谷歌地图api V2。当我加载地图时,它加载得非常完美 然而,当我试图从 xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_p
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
</RelativeLayout>
只有在谷歌地图可用的情况下,才能使用CameraUpdateFactory 我会试试下面的,把你的 私有GoogleMap=fragment.getMap() 在onCreate方法上。如果映射不为null,则可以调用CameraUpdateFactory
map = fragment.getMap();
if(map != null){
map.moveCamera(CameraUpdateFactory.newLatLngZoom(SYDNEY,15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomIn());
// Zoom out to zoom level 10, animating with a duration of 2 seconds.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
// Construct a CameraPosition focusing on Mountain View and animate the camera to that position.
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(MOUNTAIN_VIEW) // Sets the center of the map to Mountain View
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
只有在谷歌地图可用的情况下,才能使用CameraUpdateFactory 我会试试下面的,把你的 私有GoogleMap=fragment.getMap() 在onCreate方法上。如果映射不为null,则可以调用CameraUpdateFactory
map = fragment.getMap();
if(map != null){
map.moveCamera(CameraUpdateFactory.newLatLngZoom(SYDNEY,15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomIn());
// Zoom out to zoom level 10, animating with a duration of 2 seconds.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
// Construct a CameraPosition focusing on Mountain View and animate the camera to that position.
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(MOUNTAIN_VIEW) // Sets the center of the map to Mountain View
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
我认为这些就是你代码中的问题
SupportMapFragment fragment = new SupportMapFragment();
private GoogleMap map = fragment.getMap();
对象片段为空,您的GoogleMap对象可能为空。你应该这样做
SupportMapFragment sMapFragment;
GoogleMap map;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//get the MapFragment in your layout
fragment =(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
//get the GoogleMap object
map = fragment.getMap();
}
现在你们都准备好在地图上做点什么了。示例缩放到特定位置
if(map!=null){
LatLng SYDNEY = new LatLng(-33.88,151.21);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 16);
map.animateCamera(cameraUpdate);
}
我希望这有帮助。快乐编码:)我认为这就是您代码中的问题
SupportMapFragment fragment = new SupportMapFragment();
private GoogleMap map = fragment.getMap();
对象片段为空,您的GoogleMap对象可能为空。你应该这样做
SupportMapFragment sMapFragment;
GoogleMap map;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//get the MapFragment in your layout
fragment =(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
//get the GoogleMap object
map = fragment.getMap();
}
现在你们都准备好在地图上做点什么了。示例缩放到特定位置
if(map!=null){
LatLng SYDNEY = new LatLng(-33.88,151.21);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 16);
map.animateCamera(cameraUpdate);
}
我希望这有帮助。快乐编码:)如果不提供错误消息,很难提供帮助。使用Logcat调试应用程序。@joaoprudencio编辑:)如果不提供错误消息,很难提供帮助。使用Logcat调试应用程序。@joaoprudencio编辑:)好!我错过了manifest.xml中的一个标记!现在它没有摇篮,但相机没有设置在我在代码上指定的位置OK!我错过了manifest.xml中的一个标记!现在它没有摇篮,但相机没有设置在我在代码中指定的位置