Google 地图在空白 Activity 上的实施无效
Implementation of Google Map on Blank Activity Not working
我从 3 天起就陷入了这个问题。我想在 android 工作室创建的空白 activity 上实现 Google Maps
。到目前为止我所做的是
- 在 google 控制台上获得 Google API 密钥
- 将应用程序名称和 SHA 放入 google 控制台上的该项目
- 在 Value 文件夹下创建一个
xml
并将 API 键放在那里
添加Google播放依赖喜欢
compile 'com.google.android.gms:play-services:8.1.0'
在 main_activity.xml
文件中添加片段
在 AndroidMinifist.xml
文件中添加权限
但是 当我 运行 应用程序显示没有地图的片段
这是我的文件
Minifist 文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pro.soft.inzi.mapapp11">
<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="your.package.name.permission.MAPS_RECEIVE" />
<!-- The following two permissions are required for location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MapMain"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<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="@string/google_maps_key" />
</application>
</manifest>
依赖关系
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:design:23.1.0'
compile 'com.google.android.gms:play-services:8.1.0'
compile 'com.android.support:support-v4:23.1.0'
}
Map_main.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.pro.soft.inzi.mapapp11.MapMain"
tools:showIn="@layout/activity_map_main">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/map" />
</LinearLayout>
</RelativeLayout>
和MapMian.java
public class MapMain extends AppCompatActivity {
GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_map_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
怎么办请帮忙看看哪里出了问题。我这样做是因为 Google 地图模板 activity 没有显示标题栏,而且我也没有找到有用的解决方案。
任何建议/帮助。
我认为几乎一切都很好,您的代码应该可以工作。
如果仍然无法正常工作,请重新启动 IDE
这可能对您有用。
And 即使您的代码在重启后不起作用 then
在 XML
文件中,使用第一行而不是第二行作为
class="com.google.android.gms.maps.SupportMapFragment"
// instead of
android:name="com.google.android.gms.maps.MapFragment"
并且在你的 main class 中,使用:
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
而不是:
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
希望它对你有用。
我从 3 天起就陷入了这个问题。我想在 android 工作室创建的空白 activity 上实现 Google Maps
。到目前为止我所做的是
- 在 google 控制台上获得 Google API 密钥
- 将应用程序名称和 SHA 放入 google 控制台上的该项目
- 在 Value 文件夹下创建一个
xml
并将 API 键放在那里 添加Google播放依赖喜欢
compile 'com.google.android.gms:play-services:8.1.0'
在
main_activity.xml
文件中添加片段在
AndroidMinifist.xml
文件中添加权限
但是 当我 运行 应用程序显示没有地图的片段
这是我的文件
Minifist 文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pro.soft.inzi.mapapp11">
<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="your.package.name.permission.MAPS_RECEIVE" />
<!-- The following two permissions are required for location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MapMain"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<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="@string/google_maps_key" />
</application>
</manifest>
依赖关系
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:design:23.1.0'
compile 'com.google.android.gms:play-services:8.1.0'
compile 'com.android.support:support-v4:23.1.0'
}
Map_main.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.pro.soft.inzi.mapapp11.MapMain"
tools:showIn="@layout/activity_map_main">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/map" />
</LinearLayout>
</RelativeLayout>
和MapMian.java
public class MapMain extends AppCompatActivity {
GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_map_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
怎么办请帮忙看看哪里出了问题。我这样做是因为 Google 地图模板 activity 没有显示标题栏,而且我也没有找到有用的解决方案。
任何建议/帮助。
我认为几乎一切都很好,您的代码应该可以工作。
如果仍然无法正常工作,请重新启动 IDE
这可能对您有用。
And 即使您的代码在重启后不起作用 then
在 XML
文件中,使用第一行而不是第二行作为
class="com.google.android.gms.maps.SupportMapFragment"
// instead of
android:name="com.google.android.gms.maps.MapFragment"
并且在你的 main class 中,使用:
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
而不是:
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
希望它对你有用。