簡體   English   中英

Google Map正在加載,但標記未在Android本機應用程序的地圖上顯示

[英]Google Map is getting loaded but marker is not getting shown on the map in android native app

我剛剛在Google地圖上為加法器編寫了一個簡單程序。 當我在移動設備上運行該應用程序時,該標記不可見,但Google地圖正在加載。 請幫幫我。 我根據Google文檔編寫了該程序。 https://developers.google.com/maps/documentation/android/marker#add_a_marker )。 這是代碼。

主要活動

            public class MainActivity extends Activity {

                GoogleMap mMap;
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
                    if(mMap== null){
                        mMap= ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

                    }
                    mMap.addMarker(new MarkerOptions()
                    .position(new LatLng(0, 0))
                    .title("Hello world"));

                }

                @Override
                public boolean onCreateOptionsMenu(Menu menu) {
                    // Inflate the menu; this adds items to the action bar if it is present.
                    getMenuInflater().inflate(R.menu.main, menu);
                    return true;
                }

            }

Activity_main.xml

    <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"
      xmlns:map="http://schemas.android.com/apk/res-auto"
      map:cameraBearing="112.5"
      map:cameraTargetLat="-33.796923"
      map:cameraTargetLng="150.922433"
      map:cameraTilt="30"
      map:cameraZoom="6"
      map:mapType="normal"
      map:uiCompass="false"
      map:uiRotateGestures="true"
      map:uiScrollGestures="false"
      map:uiTiltGestures="true"
      map:uiZoomControls="false"
      map:uiZoomGestures="true"/>

Manifest.xml

        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
            package="com.example.dummymaptest"
            android:versionCode="1"
            android:versionName="1.0" > 
            <permission
                  android:name="com.example.dummymaptest.permission.MAPS_RECEIVE"
                  android:protectionLevel="signature"/>
                <uses-permission android:name="com.example.dummymaptest.permission.MAPS_RECEIVE"/>
            <uses-sdk
                android:minSdkVersion="12"
                android:targetSdkVersion="18" />
            <uses-permission android:name="android.permission.INTERNET" />
            <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
            <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
            <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
            <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
            <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

            <uses-feature
                android:glEsVersion="0x00020000"
                android:required="true"/>


            <application
                android:allowBackup="true"
                android:icon="@drawable/ic_launcher"
                android:label="@string/app_name"
                android:theme="@style/AppTheme" >

                <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="key" />


                <meta-data android:name="com.google.android.gms.version" 
               android:value="@integer/google_play_services_version" />
                <activity
                    android:name="com.example.dummymaptest.MainActivity"
                    android:label="@string/app_name" >
                    <intent-filter>
                        <action android:name="android.intent.action.MAIN" />

                        <category android:name="android.intent.category.LAUNCHER" />
                    </intent-filter>
                </activity>
            </application>

        </manifest>

您尚未創建標記對象。 像這樣做:

Marker marker = mMap.addMarker(new MarkerOptions()
                .position(new LatLng(0, 0))
                .title("Hello world"));

標記將顯示在非洲附近的某個地方。

您正在添加緯度為0,0的標記,因此該標記會位於非洲附近海域中的某處。 給出一些實際的經緯度,標記一定會顯示出來。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM