【问题标题】:Unable to view googlemap in Maps activity无法在地图活动中查看 googlemap
【发布时间】:2017-05-01 16:17:19
【问题描述】:

我正在尝试构建一个基于位置的 MAP 应用程序,当我尝试执行我的应用程序时,我可以查看添加到 Mapsactivity 的标记,但是我没有在 Activity 上映射

我可以确认我已经为我的活动提供了正确的 Map 密钥,从 console.developer.com 获取密钥并启用 Google Maps for Android API

请提出这里可能出现的问题

地图活动

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
private final String TAG = "MapActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    Log.d(TAG, "inside create Fragment");

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    Log.d(TAG, "outside create Fragment");
}

@Override
public void onMapReady(GoogleMap map) {
    SharedPreferences preferences= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    Double mLat = Double.parseDouble(preferences.getString("latitude",""));
    Double mLon = Double.parseDouble(preferences.getString("longtitude",""));
    Log.d(TAG, preferences.getString("latitude",""));
    Log.d(TAG, preferences.getString("longtitude",""));
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mLat,mLon), 20));
    map.addMarker(new MarkerOptions().position(new LatLng(mLat,mLon)));
    //map.setMyLocationEnabled(true);
}

}

Android 清单

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <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.gms.version"
        android:value="@integer/google_play_services_version"/>

    <activity android:name=".MyGames"/>

    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key"/>

    <activity
        android:name=".MapsActivity"
        android:label="@string/title_activity_maps">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.android.wesport.MainActivity"/>
    </activity>
</application>

Activity_maps.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:map="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/map"
      android:name="com.google.android.gms.maps.SupportMapFragment"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".MapsActivity"/>

来自模拟器的图像

【问题讨论】:

  • 您看到标记但没有看到地图?
  • @Blackkara ,是的,你是仪式。标记添加在正确的位置,但地图没有显示,我只是得到一个空白背景
  • 您的问题与缩放级别 (20) 有关吗?所以你只是看到由于缩放而导致的灰色区域?
  • @Blackkara 我刚刚将缩放级别更改为 10,但它仍然保持不变
  • 确保您的 api 密钥正常

标签: android google-maps google-maps-markers google-maps-android-api-2


【解决方案1】:

可能你在地图上显示沙漠。

尝试改变:

map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mLat,mLon), 20));

map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mLat, mLon), 5));

【讨论】:

  • 这有帮助。我提供了 20 的原始缩放,甚至将其更改为 10,但仍然无法看到地图,因为我的模拟器位置完全不同
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-15
  • 1970-01-01
  • 2012-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多