2013-05-15 57 views
0

我閱讀教程Google Maps V2並實現所有,但是當我運行應用程序時,我看不到標記,我不明白爲什麼。我想添加一個位置爲'PLACE'的標記,然後添加它,但不顯示。Android我沒有看到谷歌地圖中的標記

我認爲這個問題是在這一行:

mMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

因爲當我加入「MapFragment.newInstance」地圖顯示位置「地」,但是如果我不「的MapOptions()」不添加此選項,地圖顯示正常。

有人可以幫忙嗎?謝謝你,並對英語感到抱歉。

我代碼:

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"/> 
+1

是地圖顯示在你的應用程序中嗎? – TheFlash

+0

爲什麼你調用addMarker()方法兩次,一次使用圖標功能,另一次沒有使用相同LatLng的圖標? –

+0

是,地圖顯示在我的應用程序,以正確的選項,但沒有 – nss

回答

0

嘗試增加標記

mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(38.977,-9.4185), 15)); 

     // Zoom in, animating the camera. 
     mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); 

後將此代碼添加這應該可以拿你去了馬rker position

+1

謝謝標誌,我加你的代碼,我沒有看到標記,我認爲問題出現在這一行:mMap =((MapFragment)getFragmentManager()。findFragmentById(R.id.map))。getMap(); 因爲當我添加「的MapOptions()」中的「MapFragment.newInstance」地圖顯示位置「地」,但如果我不加這個選項,地圖顯示正常 – nss

+0

謝謝,我解決問題,問題是我用這一行創建一個新的地圖:MapFragment mMapFragment = MapFragment.newInstance(mapOptions());當我調用getMap()時,出現的地圖就是一個MapFragment。 – nss