我正在努力在一个小应用中集成谷歌地图。我的AndroidManifest.xml
是
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mymaps"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="com.example.mymaps.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<uses-permission
android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE" />
<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-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature android:glEsVersion="0x00020000"/>
<uses-feature android:required="true"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.mymaps.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>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="MyKey" />
</application>
</manifest>
我的主要活动是
public class MainActivity extends Activity implements OnClickListener {
TextView lat,lng;
Button getCds,getMap;
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeVars();
}
private void initializeVars() {
// TODO Auto-generated method stub
getCds = (Button)findViewById(R.id.bGC);
getMap = (Button)findViewById(R.id.bGM);
lat = (TextView)findViewById(R.id.TextView2);
lng = (TextView)findViewById(R.id.TextView4);
getCds.setOnClickListener(this);
getMap.setOnClickListener(this);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
}
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
public void onClick(View v) {
Log.d("Debugging", "I am in onCLick");
switch (v.getId()) {
case R.id.bGC:
{
Log.d("Debugging", "I am in case R.id.bGC");
lat.setText("My Lat");
lng.setText("My Long");
break;
}
case R.id.bGM:
{
Log.d("Debugging", "I am in case R.id.bGM");
lat.setText("blank");
lng.setText("blank");
break;
}
}
}
}
当我加载应用程序时,我收到以下错误。
01-17 14:01:49.054: E/Google Maps Android API(2832): Authorization failure. Please see https://developers.google.com/maps/documentation/android/start for how to correctly set up the map.
01-17 14:01:49.064: E/Google Maps Android API(2832): Ensure that the following correspond to what is in the API Console: Package Name: com.example.mymaps, API Key: MyKey, Certificate Fingerprint: SomeValue
01-17 14:01:49.064: I/Google Maps Android API(2832): Failed to contact Google servers. Another attempt will be made when connectivity is established.
01-17 14:01:59.234: D/dalvikvm(2832): GC_CONCURRENT freed 572K, 9% free 7576K/8263K, paused 6ms+3ms
01-17 14:02:04.304: E/Google Maps Android API(2832): Failed to load map. Error contacting Google servers. This is probably an authentication issue (but could be due to network errors).
伙计们提供更多的观点。以前用过相同的代码(大概3-4个月后)。它似乎不再起作用了。我不确定是什么造成的。
你能告诉你如何调试它吗?
答案 0 :(得分:1)
从
进行更改<uses-permission
android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE" />
到
<uses-permission
android:name="com.example.mymaps.permission.MAPS_RECEIVE" />
我认为问题出在密钥上,并且它不是您正在使用的密钥,您的清单中的API密钥显然与您在API控制台中显示的API密钥不匹配。将API密钥从控制台粘贴到清单中。应该解决你的问题。
你可以卸载应用程序,然后清理一个项目,然后重新安装应用程序..试试这个但是它仍然无效
了解更多 请访问有关使用新谷歌地图的完整流程
http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/
http://www.learn2crack.com/2013/12/android-google-maps-api-v2-example.html
最好的教程是
http://codebybrian.com/2012/12/06/google_maps_android_v2_sample.html
答案 1 :(得分:0)
不再需要your.package.name.permission.MAPS_RECEIVE
。请从清单中删除它。你也错过了这个:
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
应添加到<application>
标记内。