【问题标题】:google map in android applicationAndroid应用程序中的谷歌地图
【发布时间】:2011-02-01 11:29:49
【问题描述】:

我是 Android 新手。我已经尝试了很多天 基本的谷歌地图应用程序,但还无法完成... :(
代码没有错误,模拟器从终端运行良好,地图 钥匙也很好,但我还是看不到地图。当我运行我的 仅应用程序网格出现,地图不显示。这是代码,可以 请任何人帮助我。

public class HelloGoogleMaps extends MapActivity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        MapView mapView =(MapView)findViewById(R.id.mapview); 
        mapView.setBuiltInZoomControls(true); 
    } 
    protected boolean isRouteDisplayed(){ 
        return false; 
    } 
} 

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<com.google.android.maps.MapView 
            xmlns:android="http://schemas.android.com/apk/res/android" 
            android:id="@+id/mapview" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent" 
            android:layout_weight="1" 
            android:clickable="true" 
            android:apiKey="0fyF-qSuCtdQinoUGoFbLxZoTx10Tm-YV6m6A8g" 
/> 

清单文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.android.GoogleMaps" 
    android:versionCode="1" 
    android:versionName="1.0"> 

    <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"/> 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
        <uses-library android:name="com.google.android.maps" /> 
        <uses-permission android:name="android.permission.INTERNET"/> 
        <activity android:name=".HelloGoogleMaps" 
              android:label="@string/app_name" 
              android:theme="@android:style/Theme.NoTitleBar"> 
        <!--<activity android:name=".HelloGoogleMaps" 
                  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> 
    <uses-sdk android:minSdkVersion="4" /> 
</manifest> 

不知道哪里出了问题。我正在使用 eclipse 和 android 1.6

【问题讨论】:

  • 通知栏有3G图标吗?谷歌地图需要互联网才能上班。
  • [1] 验证您使用的是正确的 API 密钥。 [2] 仔细检查您是否在 AndroidMenifest.xml 中添加了必要的 INTERNET 权限
  • @Sarwar Erfan,她在清单中有 INTERNET 权限,文件已列出。
  • @Vladimir Ivanov:只是指出常见错误。没有给出任何答案。它的评论部分:)

标签: android google-maps


【解决方案1】:

尝试在应用标签之外设置互联网权限

【讨论】:

    【解决方案2】:
        **// Activty**
         public class MapsActivity extends MapActivity {
    
                private MapController mapController;
                private MapView mapView;
                private LocationManager locationManager;
                private MyOverlays itemizedoverlay;
                private MyLocationOverlay myLocationOverlay;
    
                public void onCreate(Bundle bundle) {
                    super.onCreate(bundle);
                    setContentView(R.layout.main); // bind the layout to the activity
    
                    // Configure the Map
                    mapView = (MapView) findViewById(R.id.map_container);
                    mapView.setBuiltInZoomControls(true);
                //  mapView.setSatellite(true);
                    mapView.setStreetView(true);
    
                    mapController = mapView.getController();
                    mapController.setZoom(14); // Zoon 1 is world view
                    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                            0, new GeoUpdateHandler());
    
                    myLocationOverlay = new MyLocationOverlay(this, mapView);
                    mapView.getOverlays().add(myLocationOverlay);
    
                    myLocationOverlay.runOnFirstFix(new Runnable() {
                        public void run() {
                            mapView.getController().animateTo(myLocationOverlay.getMyLocation());
                        }
                    });
    
                    Drawable drawable = this.getResources().getDrawable(R.drawable.map);
                    itemizedoverlay = new MyOverlays(this, drawable);
                    createMarker();
                }
    
    
                protected boolean isRouteDisplayed() {
                    return false;
                }
    
                public class GeoUpdateHandler implements LocationListener {
    
    
                    public void onLocationChanged(Location location) {
                        int lat = (int) (location.getLatitude() * 1E6);
                        int lng = (int) (location.getLongitude() * 1E6);
                        GeoPoint point = new GeoPoint(lat, lng);
                        createMarker();
                        mapController.animateTo(point); // mapController.setCenter(point);
    
                    }
    
    
                    public void onProviderDisabled(String provider) {
                    }
    
    
                    public void onProviderEnabled(String provider) {
                    }
    
    
                    public void onStatusChanged(String provider, int status, Bundle extras) {
                    }
                }
    
                private void createMarker() {
                    GeoPoint p = mapView.getMapCenter();
                    OverlayItem overlayitem = new OverlayItem(p, "", "");
                    itemizedoverlay.addOverlay(overlayitem);
                    if (itemizedoverlay.size() > 0) {
                        mapView.getOverlays().add(itemizedoverlay);
                    }
                }
    
    
                protected void onResume() {
                    super.onResume();
                    myLocationOverlay.enableMyLocation();
                    myLocationOverlay.enableCompass();
                }
    
    
                protected void onPause() {
                    super.onPause();
                    myLocationOverlay.disableMyLocation();
                    myLocationOverlay.disableCompass();
                }
            } 
    
        **//Class MyOvelays**
    
            public class MyOverlays extends ItemizedOverlay<OverlayItem> {
    
                private static int maxNum = 5;
                private OverlayItem overlays[] = new OverlayItem[maxNum];
                private int index = 0;
                private boolean full = false;
                private Context context;
                private OverlayItem previousoverlay;
    
                public MyOverlays(Context context, Drawable defaultMarker) {
                    super(boundCenterBottom(defaultMarker));
                    this.context = context;
                }
    
                @Override
                protected OverlayItem createItem(int i) {
                    return overlays[i];
                }
    
                @Override
                public int size() {
                    if (full) {
                        return overlays.length;
                    } else {
                        return index;
                    }
    
                }
    
                public void addOverlay(OverlayItem overlay) {
                    if (previousoverlay != null) {
                        if (index < maxNum) {
                            overlays[index] = previousoverlay;
                        } else {
                            index = 0;
                            full = true;
                            overlays[index] = previousoverlay;
                        }
                        index++;
                        populate();
                    }
                    this.previousoverlay = overlay;
                }
    
                protected boolean onTap(int index) {
                    OverlayItem overlayItem = overlays[index];
                    Builder builder = new AlertDialog.Builder(context);
                    builder.setMessage("This will end the activity");
                    builder.setCancelable(true);
                    builder.setPositiveButton("I agree", new OkOnClickListener());
                    builder.setNegativeButton("No, no", new CancelOnClickListener());
                    AlertDialog dialog = builder.create();
                    dialog.show();
                    return true;
                };
    
                private final class CancelOnClickListener implements
                        DialogInterface.OnClickListener {
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(context, "You clicked yes", Toast.LENGTH_LONG)
                                .show();
                    }
                }
    
                private final class OkOnClickListener implements
                        DialogInterface.OnClickListener {
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(context, "You clicked no", Toast.LENGTH_LONG).show();
                    }
                }
            } 
    
    
        //Main.xml
    
            <?xml version="1.0" encoding="utf-8"?>
            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent">
    
    
    
             <com.google.android.maps.MapView
    
                            android:id="@+id/map_container"
                             android:layout_width="fill_parent"
                             android:layout_height="fill_parent"
                             android:apiKey="0PYAmXmindXBuvCvIFhCUC3y0GNjJKuFJHclkVw"
                             android:clickable="true"
                             android:focusable="true"
                             android:keepScreenOn="true"
    
    
                             />
    
    
            </RelativeLayout>
    
        //Android.mainifest
    
    
    
     <?xml version="1.0" encoding="utf-8"?>
            <manifest xmlns:android="http://schemas.android.com/apk/res/android"
                package="com.map"
                android:versionCode="1"
                android:versionName="1.0" >
    
                <uses-sdk android:minSdkVersion="10" />
                <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"/>
    
    
                <application
                    android:icon="@drawable/ic_launcher"
                    android:label="@string/app_name" >
                     <uses-library android:name="com.google.android.maps" />
                    <activity
                        android:name=".MapsActivity"
                        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>
    

    【讨论】:

    • 我已经使用了这段代码,这可以正常地在 android 中创建谷歌地图
    【解决方案3】:

    您是否使用了正确的 Google 地图密钥 - 如果您使用调试密钥,地图似乎无法正常工作。

    您可能必须在没有 --debug 标志的情况下重新生成密钥。

    也许这会有所帮助:Android MapView - tiles not loading with Debug API key

    【讨论】:

      【解决方案4】:

      常见问题是您使用了错误的模拟器(即不是 Google API)、导入错误、API 密钥错误、清单中没有权限、没有使用库等

      我为此写了一个新手指南,看看 http://www.jameselsey.co.uk/blogs/techblog/android-how-to-display-a-map-the-easy-way/

      【讨论】:

        【解决方案5】:

        我最近经历了所有这些。我也尝试了所有提到的,但所有的工作是 -

        确保您不是代理,并删除您在模拟器上所做的任何代理设置。存在一个错误,即 Google 地图无法在代理后面的 Android 模拟器上运行。

        【讨论】:

          【解决方案6】:

          我认为问题在于您使用了错误的 apk。您必须使用模拟器生成的 apk(在我的例子中是 eclipse),而不是使用选项“export”生成的 apk。我认为问题与用于导出 apk 的密钥与 google maps api 密钥不同。试试看。

          PS:对不起我的英语不好。

          【讨论】:

            猜你喜欢
            • 2014-10-29
            • 2014-09-17
            • 1970-01-01
            • 2023-03-21
            • 1970-01-01
            • 2015-09-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多