我想让Google地图在Android中使用,但我遇到了一些问题。
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.odv"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="YOUR_PACKAGE_NAME.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="your.package.name.permission.MAPS_RECEIVE" />
<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" />
<!-- Maps API needs OpenGL ES 2.0. -->
<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" >
<!-- You must insert your own Google Maps for Android API v2 key in here. -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCe29ye5iK83zx6rUBnCzulKCxf68nT-bs" />
<activity
android:name="com.example.odv.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>
主XML:
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2012 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- This can go anywhere in your layout (see other demos for some examples).
<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.SupportMapFragment"/>
-->
<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.SupportMapFragment" />
Mainactivity:
package com.example.odv;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends android.support.v4.app.FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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;
}
}
我想学习Android。我想我有编程的算法智能,但我不知道如何开始学习编程。我没有一个好的来源。
我写了这段代码,但我看不到地图。我只能看到一个白色的屏幕,无法打开地图。我能为此做些什么?
截图
答案 0 :(得分:0)
您需要做的第一件事是,从API CONSOLE获取API密钥,然后您需要在Android Manifest中使用该API密钥: 1使用适当的权限:
第二:使用功能
android:glEsVersion="0x00020000"
android:required="true" >
第三: 在元数据中:
机器人:名称= “com.google.android.maps.v2.API_KEY” android:value =“你的钥匙在这里”&gt;
希望这会有所帮助.. :)
答案 1 :(得分:0)
公共类MainActivity扩展了FragmentActivity {
private static final int GPS_ERRORDIALOG_REQUEST = 9001;
Button btn1;
EditText et1;
EditText et2;
GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (servicesOK()) {
Toast.makeText(this, "Ready to map!!", Toast.LENGTH_LONG).show();
setContentView(R.layout.testmap);
} else {
setContentView(R.layout.activitymain);
}
/*
* et1 = (EditText)findViewById(R.id.edittext1); et2 =
* (EditText)findViewById(R.id.edittext2); btn1 =
* (Button)findViewById(R.id.button1);
*
* btn1.setOnClickListener(this);
*/
}
@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;
}
/*
* @Override public void onClick(View v) {
*
* Object lat = et1; Object lon = et2;
*
* Uri uri=Uri.parse("geo:"+lat.toString()+","+lon.toString()); Intent i=new
* Intent(Intent.ACTION_VIEW, uri); startActivity(i); Log.d("The Location:",
* lat.toString()+lon.toString());
*
*
* }
*/
public boolean servicesOK() {
int isAvailable = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
} else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable,
this, GPS_ERRORDIALOG_REQUEST);
dialog.show();
} else {
Toast.makeText(this, "Cant connect!!", Toast.LENGTH_SHORT).show();
}
return false;
}
}
另外,我只是布局中的mapview,请不要忘记将API KEY放在清单中,因为没有它,地图将不会加载。并使用地图,请使用真实的设备,而不是模拟器.. :)
答案 2 :(得分:0)
以下是我在测试应用中使用的地图的完整工作:
公共类MainActivity扩展了FragmentActivity {
private static final int GPS_ERRORDIALOG_REQUEST = 9001;
Button btn1;
EditText et1;
EditText et2;
GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (servicesOK()) {
Toast.makeText(this, "Ready to map!!", Toast.LENGTH_LONG).show();
setContentView(R.layout.testmap);
} else {
setContentView(R.layout.activitymain);
}
/*
* et1 = (EditText)findViewById(R.id.edittext1); et2 =
* (EditText)findViewById(R.id.edittext2); btn1 =
* (Button)findViewById(R.id.button1);
*
* btn1.setOnClickListener(this);
*/
}
@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;
}
/*
* @Override public void onClick(View v) {
*
* Object lat = et1; Object lon = et2;
*
* Uri uri=Uri.parse("geo:"+lat.toString()+","+lon.toString()); Intent i=new
* Intent(Intent.ACTION_VIEW, uri); startActivity(i); Log.d("The Location:",
* lat.toString()+lon.toString());
*
*
* }
*/
public boolean servicesOK() {
int isAvailable = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
} else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable,
this, GPS_ERRORDIALOG_REQUEST);
dialog.show();
} else {
Toast.makeText(this, "Cant connect!!", Toast.LENGTH_SHORT).show();
}
return false;
}
}
/////////////////////////////////////////////// ////////////////////////////////////////
这是布局:
xmlns:maps="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
这是Android Manifest:
package="com.mike.maps"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="18" />
<permission
android:name="com.mike.maps.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.mike.maps.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" >
</uses-feature>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.mike.maps.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="com.mike.maps.SecondActivity" >
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="Your key goes here" >
</meta-data>
</application>