Google Maps Marker not getting displayed

2019-06-08 14:38发布

I am trying to learn google map API. I see a map but I am unable to get a marker at the right location. I see it somewhere in the ocean near China. Moreover, map is centered at (0,0) and not the (lat,long) that I have provided. Here is my code

Activity

public class HomeActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_page);
        GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        map.addMarker(new MarkerOptions()
                .position(new LatLng(37.7750, 122.4183))
                .title("San Francisco"));
    }

}

home_page.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"/>

AndroidManifest.xml

  <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="16" />

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-feature
            android:glEsVersion="0x00020000"
            android:required="true"/>
    <uses-feature android:name="android.hardware.camera"
                  android:required="true" />
    <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"/>


    <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.gms.version"
                android:value="@integer/google_play_services_version" />
        <activity android:name=".HomeActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="My SHA1 key"/>
    </application>

What am I missing out?

4条回答
劫难
2楼-- · 2019-06-08 14:47

The lat, long of San Francisco is 37.7833° N, 122.4167° W according to Google. A longitude on the West should be indicated with a negative sign. So the coordinates should have been something like 37.7750, -122.4183

查看更多
\"骚年 ilove
3楼-- · 2019-06-08 14:49

Initially your GoogleMap can takes times to load. So just check by this.

 if(map!=null) {

   map.addMarker(new MarkerOptions()
            .position(new LatLng(37.7750, 122.4183))
            .title("San Francisco"));

 }
查看更多
Deceive 欺骗
4楼-- · 2019-06-08 14:49

You can use zoomToSpan

This will set center to your location. I think this what you need.

查看更多
淡お忘
5楼-- · 2019-06-08 14:59

I think, you need to set icon to show marker. So, added .icon and set your own marker icon or use default icon.

map.addMarker(new MarkerOptions()
                .position(new LatLng(37.7750, 122.4183))
                .title("San Francisco").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
查看更多
登录 后发表回答