2011-02-01 135 views
0

我是Android新手。我一直试图从很多天,使非常 基本的谷歌地图应用程序,但无法完成它呢... :(
有没有错误的代码,模拟器从终端运行良好,地图 键还行,但我仍然无法在地图上看到,当我跑我的 程序只显示网格,并且不显示的地图,这里是代码,可以 任何机构请帮我。谷歌地图在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

+0

通知栏中是否有3G图标?谷歌地图需要互联网才能上班。 – 2011-02-01 11:33:27

+0

[1]验证您使用的是正确的API密钥。 [2]仔细检查你是否在AndroidMenifest.xml中添加了必要的INTERNET权限 – 2011-02-01 11:36:45

+0

@Sarwar Erfan,她在清单中有INTERNET权限,文件被列出。 – 2011-02-01 11:40:04

回答

1

尝试设置应用程序标记之外Internet权限

0

我认为问题在于您使用的是错误的apk。你必须使用由模拟器生成的apk(我的例子中的eclipse),而不是使用选项“export”生成的apk。我认为这个问题是关系到用来导出apk的密钥不同于google maps api key的。尝试一下。 PS:对不起,我的英语不好。

0

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

请确保你不是一个代理的背后,并删除您在模拟器上进行的任何代理设置。 Google地图在代理服务器后面的Android模拟器上不起作用。

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> 
相关问题