我正在尝试使用Google地图创建一个WebView应用程序,但是当我运行它时,它只显示一张空白地图。
这是我的MainActivity.java
public class MainActivity extends ActionBarActivity {
public WebView mWB;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
setContentView(R.layout.activity_main);
mWB = (WebView) findViewById(R.id.mWB);
mWB.setWebViewClient(new MCWC());
WebSettings webSET = mWB.getSettings();
webSET.setJavaScriptEnabled(true);
mWB.loadUrl("http://www.tomshardware.com/");
Button btnEins = (Button) findViewById(R.id.bntmap);
btnEins.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intencion = new Intent(v.getContext(),maps.class );
startActivity(intencion);
}
});
}
public class MCWC extends WebViewClient {
public boolean ShouldOverrideLoading(String url,WebView view){
view.loadUrl(url);
return true;
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (mWB.canGoBack()) {
mWB.goBack();
} else {
finish();
}
return true;
}
/**@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_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);
}**/
}
return false;
}
}
这是maps.java
public class maps extends Activity implements OnMapReadyCallback {
UiSettings mapSettings;
private final LatLng GEVGELIA = new LatLng(41.1421756,22.5026124);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maps);
GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(GEVGELIA,15);
map.animateCamera(update);
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.setMyLocationEnabled(true);
map.setBuildingsEnabled(true);
map.addMarker(new MarkerOptions()
.position(new LatLng(41.139560, 22.503117))
.title("Korzo"))
.isVisible();
map.addMarker(new MarkerOptions()
.position(new LatLng(41.142284, 22.504579))
.title("SOU Josif Josifofski"))
.isVisible();
}
public void onMapReady(GoogleMap map) {
mapSettings.setZoomControlsEnabled(true);
mapSettings.setTiltGesturesEnabled(true);
mapSettings.setRotateGesturesEnabled(true);
mapSettings.setMyLocationButtonEnabled(true);
map.addMarker(new MarkerOptions()
.position(new LatLng(0, 0))
.title("Marker"));
}
}
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gvglive.gvglive" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<permission
android:name="com.gvglive.gvglive.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.barcodelibrary.permission.MAPS_RECEIVE"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<uses-permission android:name="android.permission.INTERNET"/>
<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"/>
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<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:theme="@style/AppTheme" >
<activity
android:name=".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>
<activity
android:name=".maps"
android:label="@string/app_name">
<category android:name="android.intent.category.LAUNCHER" />
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBEzDPkkL7c1VW9HS9KC8awuRsv9UpuA0M"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
maps.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
tools:context=".MainActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mWB">
</WebView>
<Button
android:id="@+id/bntmap"
style="?android:attr/buttonStyleSmall"
android:layout_width="55dp"
android:layout_height="55dp"
android:text="MAP"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:drawableTop="@drawable/top"
android:background="@drawable/top"/>
</RelativeLayout>
将此添加到build.gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.google.android.gms:play-services:6.5.87'
}
我已经检查了我的SHA1指纹和API密钥, 我甚至尝试重新生成一个新的API密钥, 我必须提一下,当我从android studio运行它到我的设备时它会显示地图,但是当我生成一个apk并安装它时,没有显示地图,我也尝试了app-release和app-debug。 / p>
答案 0 :(得分:0)
您可以使用SDK导入中的Google Play服务库直接使用地图服务,并通过项目属性将其引用。或者,如果您使用的是Android Studio,只需使用默认的Maps项目创建项目即可。
请参阅以下工作代码...您可以按原样复制和粘贴文件:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.avalunjkar.mymaps" >
<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" />
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but are recommended.
-->
<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" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MapsActivity.java
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}
activitmaps.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:id="@+id/map" tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
请更改软件包名称并在Strings.xml或Manifest.xml中添加API V2密钥。