我正在尝试这个约4个小时,但没有发生。试图开发一个应用程序来显示谷歌地图中的特定位置。
以下代码:
GooglemapsActivity.java
package com.example.gps2;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import android.os.Bundle;
public class GooglemapsActivity extends MapActivity {
private MapView mapView;
private MapController mc;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
mc = mapView.getController();
String coordinates[] = {"30", "71"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
GeoPoint p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(7);
mapView.invalidate();
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.google.android.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="AIzaSyBbGgoanloghVHc_rSHmS_yBG1Y7Ho7rsM"
/>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gps2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<permission
android:name="com.example.gps2.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:name="android.hardware.location"
android:required="false" />
<uses-feature
android:name="android.hardware.location.network"
android:required="false" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-feature
android:name="android.hardware.wifi"
android:required="false" />
<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" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name="com.example.gps2.GooglemapsActivity"
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>
我使用的是Google API 18(平台4.3)
我使用SHA1代码生成了android密钥“B5:EE:A3:80:7C:11:16:21:D7:EC:95:D1:FE:03:79:3D:4F:92: 2D:73; com.example.gps2"
我正在使用目标Google API 18的AVD。
毕竟,谷歌地图没有显示出来。只需使用网格获取屏幕。请建议我需要修复的地方才能使其正常工作。
logcat的:
01-12 07:01:20.958: D/dalvikvm(993): GC_FOR_ALLOC freed 533K, 18% free 3207K/3864K, paused 51ms, total 56ms
01-12 07:01:20.968: W/System.err(993): java.lang.IllegalStateException: Connection pool shut down.
01-12 07:01:20.968: W/System.err(993): at org.apache.http.impl.conn.tsccm.ConnPoolByRoute.getEntryBlocking(ConnPoolByRoute.java:284)
01-12 07:01:20.968: W/System.err(993): at org.apache.http.impl.conn.tsccm.ConnPoolByRoute$1.getPoolEntry(ConnPoolByRoute.java:238)
01-12 07:01:20.968: W/System.err(993): at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager$1.getConnection(ThreadSafeClientConnManager.java:176)
01-12 07:01:20.968: W/System.err(993): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:326)
01-12 07:01:20.968: W/System.err(993): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-12 07:01:20.968: W/System.err(993): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-12 07:01:20.968: W/System.err(993): at android_maps_conflict_avoidance.com.google.common.io.android.AndroidHttpClient.execute(AndroidHttpClient.java:246)
01-12 07:01:20.978: W/System.err(993): at android_maps_conflict_avoidance.com.google.common.io.android.GoogleHttpClient.executeWithoutRewriting(GoogleHttpClient.java:129)
01-12 07:01:20.978: W/System.err(993): at android_maps_conflict_avoidance.com.google.common.io.android.GoogleHttpClient.execute(GoogleHttpClient.java:189)
01-12 07:01:20.978: W/System.err(993): at android_maps_conflict_avoidance.com.google.common.io.android.GoogleHttpClient.execute(GoogleHttpClient.java:242)
01-12 07:01:20.978: W/System.err(993): at android_maps_conflict_avoidance.com.google.common.io.android.AndroidHttpConnectionFactory$AndroidGoogleHttpConnection.getResponse(AndroidHttpConnectionFactory.java:132)
01-12 07:01:20.978: W/System.err(993): at android_maps_conflict_avoidance.com.google.common.io.android.AndroidHttpConnectionFactory$AndroidGoogleHttpConnection.openDataInputStream(AndroidHttpConnectionFactory.java:153)
01-12 07:01:20.978: W/System.err(993): at android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher.serviceRequests(DataRequestDispatcher.java:922)
01-12 07:01:20.978: W/System.err(993): at android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher$DispatcherServer.run(DataRequestDispatcher.java:1702)
01-12 07:01:20.988: W/System.err(993): at java.lang.Thread.run(Thread.java:841)
01-12 07:01:31.467: W/MapActivity(993): Recycling dispatcher android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher@41702a70
01-12 07:01:31.507: V/MapActivity(993): Recycling map object.
01-12 07:01:32.387: I/MapActivity(993): Handling network change notification:CONNECTED
01-12 07:01:32.387: E/MapActivity(993): Couldn't get connection factory client
01-12 07:01:51.737: W/System.err(993): IOException processing: 26
01-12 07:01:51.737: W/System.err(993): java.io.IOException: Server returned: 3
01-12 07:01:51.748: W/System.err(993): at android_maps_conflict_avoidance.com.google.googlenav.map.BaseTileRequest.readResponseData(BaseTileRequest.java:115)
01-12 07:01:51.758: W/System.err(993): at android_maps_conflict_avoidance.com.google.googlenav.map.MapService$MapTileRequest.readResponseData(MapService.java:1473)
01-12 07:01:51.758: W/System.err(993): at android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher.processDataRequest(DataRequestDispatcher.java:1117)
01-12 07:01:51.758: W/System.err(993): at android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher.serviceRequests(DataRequestDispatcher.java:994)
01-12 07:01:51.758: W/System.err(993): at android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher$DispatcherServer.run(DataRequestDispatcher.java:1702)
01-12 07:01:51.758: W/System.err(993): at java.lang.Thread.run(Thread.java:841)
01-12 07:02:15.378: W/System.err(993): IOException processing: 26
01-12 07:02:15.378: W/System.err(993): java.io.IOException: Server returned: 3
01-12 07:02:15.378: W/System.err(993): at android_maps_conflict_avoidance.com.google.googlenav.map.BaseTileRequest.readResponseData(BaseTileRequest.java:115)
01-12 07:02:15.389: W/System.err(993): at android_maps_conflict_avoidance.com.google.googlenav.map.MapService$MapTileRequest.readResponseData(MapService.java:1473)
01-12 07:02:15.389: W/System.err(993): at android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher.processDataRequest(DataRequestDispatcher.java:1117)
01-12 07:02:15.389: W/System.err(993): at android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher.serviceRequests(DataRequestDispatcher.java:994)
01-12 07:02:15.398: W/System.err(993): at android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher$DispatcherServer.run(DataRequestDispatcher.java:1702)
01-12 07:02:15.398: W/System.err(993): at java.lang.Thread.run(Thread.java:841)
01-12 07:02:44.837: W/System.err(993): IOException processing: 26
01-12 07:02:44.837: W/System.err(993): java.io.IOException: Server returned: 3
01-12 07:02:44.837: W/System.err(993): at android_maps_conflict_avoidance.com.google.googlenav.map.BaseTileRequest.readResponseData(BaseTileRequest.java:115)
01-12 07:02:44.847: W/System.err(993): at android_maps_conflict_avoidance.com.google.googlenav.map.MapService$MapTileRequest.readResponseData(MapService.java:1473)
01-12 07:02:44.857: W/System.err(993): at android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher.processDataRequest(DataRequestDispatcher.java:1117)
01-12 07:02:44.857: W/System.err(993): at android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher.serviceRequests(DataRequestDispatcher.java:994)
01-12 07:02:44.857: W/System.err(993): at android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher$DispatcherServer.run(DataRequestDispatcher.java:1702)
01-12 07:02:44.867: W/System.err(993): at java.lang.Thread.run(Thread.java:841)
...谢谢
答案 0 :(得分:2)
您在应用程序中如何实施Google Maps API V2以及将许多内容与API V1实施相混淆时,一堆乱七八糟。
首先,要在一个活动中显示谷歌地图,你通常会使用MapFragment
或SupportMapFragment
(例如API < 11
),所以你应该在你的xml布局中添加它活性:
<fragment
android:name="com.google.android.gms.maps.SupportMapFragment"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
接下来,您应inherit
FramgnetActivity
API V2
反对MapActivity
API V1
,所以您应该这样做:
public class MapActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_layout);
}
}
最后如前所述,您应该在清单文件中提供API密钥,而不是在xml文件中。所以把它放在application
标记范围下的清单中:
<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="Your Google Maps API V2 Key" />
您可以在我撰写的关于此主题的博文中找到详细说明:
答案 1 :(得分:0)
<permission
android:name="your_package_name.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
尝试放置您的包裹名称....
答案 2 :(得分:0)
我建议您在https://developers.google.com/maps/documentation/android/start
时仔细按照设置进行操作从我所看到的你的清单中,你错过了
<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="API_KEY"/>
并且推荐的布局是片段而不是MapView等。
答案 3 :(得分:0)
您正试图在AVD中获取地图,因此您可以参考this链接了解如何在模拟器中获取地图。