Android应用程序中的谷歌地图

时间:2011-02-01 11:29:50

标签: android google-maps

我是Android新手。我一直在努力做很多天 基本的谷歌地图应用程序,但无法完成它...... :(
代码中没有错误,模拟器从终端运行正常,Map 钥匙也很好,但我仍然无法看到地图。当我跑我的 仅显示应用网格并且不显示地图。这是代码,可以 任何人都请帮帮我。

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

6 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

您使用的是正确的Google地图密钥吗?如果使用调试密钥,地图似乎无效。

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

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

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

我认为问题是你使用的是错误的apk。您必须使用模拟器生成的apk(在我的情况下是eclipse)而不是使用选项“ export ”生成的apk。我认为问题与用于导出apk的密钥有关,与google maps api密钥不同。试试吧。

PS:抱歉我的英语不好。

答案 5 :(得分:0)

我最近经历过所有这些。我也尝试了所提到的一切,但所有有效的是 -

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