我阅读了教程Google Maps V2并实现了所有,但是当我运行应用程序时,我看不到标记,我不明白为什么。我想添加一个位置为'PLACE'的标记,然后我添加它,但不显示。
我认为问题出在这一行:
mMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
因为当我在'MapFragment.newInstance'中添加'mapOptions()'时,地图会显示位置'PLACE',但如果我不添加此选项,则地图显示正常。
有人可以帮忙吗?谢谢,抱歉英语。
我的代码:
Frag_Map.java
public class Frag_Map extends Activity {
private GoogleMap mMap;
private static final LatLng PLACE = new LatLng(38.977,-9.4185);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xmlfile);
MapFragment mMapFragment = MapFragment.newInstance(mapOptions());
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.map, mMapFragment);
fragmentTransaction.commit();
setUpMapIfNeeded();
}
private GoogleMapOptions mapOptions()
{
GoogleMapOptions options = new GoogleMapOptions();
options.mapType(GoogleMap.MAP_TYPE_HYBRID)
.compassEnabled(true)
.rotateGesturesEnabled(true)
.scrollGesturesEnabled(true)
.tiltGesturesEnabled(true)
.zoomGesturesEnabled(true)
.zoomControlsEnabled(true);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(PLACE)
.zoom((float) 17.5)
.build();
options.camera(cameraPosition);
return options;
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
// The Map is verified. It is now safe to manipulate the map.
mMap.addMarker(new MarkerOptions()
.position(new LatLng(38.977,-9.4185))
.title("Hello world")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.mapaovelay1))
.visible(true));
}
}
}
xmlfile.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
答案 0 :(得分:0)
尝试在添加标记
后添加此代码 mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(38.977,-9.4185), 15));
// Zoom in, animating the camera.
mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
希望这会带你到Marker的位置