2012-06-26 47 views
-1

我想在Android中显示谷歌地图,我有地图API甚至地图不显示,也没有抛出任何错误。 我有使用以下 Java代码Android地图不显示

public class GoogleMapActivity extends MapActivity { 
    private Location myLocation; 
    protected MapView myMapView = null; 
    protected LocationManager myLocationManager = null; 
    protected MapController mapController; 
    List<Overlay> mapOverlays; 

    protected boolean isRouteDisplayed() { 
     return false; 
    } 

    protected void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     this.myMapView = new MapView(this, "0jzIB6m5R1kLa_rGte-DS9PhF3KlSgqYHUZognA");  
     this.setContentView(myMapView); 
     myMapView.setBuiltInZoomControls(true); 
     myMapView.setSatellite(true); 
     mapController = myMapView.getController(); 
     mapOverlays = myMapView.getOverlays(); 
     this.myLocation = new Location("gps"); 
     this.myLocation.setLongitude(77.52436144125092); 
     this.myLocation.setLatitude(13.05096452223662); 
     updateView(); 
    } 

    private void updateView() { 
     Double lat = myLocation.getLatitude(); 
     Double lng = myLocation.getLongitude(); 
     GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue()); 
     mapController.setCenter(point); 
    } 
} 

清单

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

    <uses-sdk android:minSdkVersion="8" /> 

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_GPS" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name=".GoogleMapActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <uses-library android:name="com.google.android.maps" /> 
    </application> 

</manifest> 

最后我得到的输出空白。请看看下面的屏幕

image

上请建议我在哪里,我错了

+0

你是你唱你自己的地图API密钥?如果不是,[在这里获取新的地图API密钥](https://developers.google.com/android/maps-api-signup) – Praveenkumar

+0

是的,这是我自己的Api密钥 – Dilip

+0

最有可能的连接问题,如果没有丢失API密钥 – reTs

回答

0

我想你还没有在当前布局中添加myMapView ...........

你在Java代码中

this.myMapView = new MapView(this, 
       "0jzIB6m5R1kLa_rGte-DS9PhF3KlSgqYHUZognA"); 

创建的,但在任何布局看起来不加..按link

  1. 如果动态地创建通过其构造的MapView,你可以 提供API密钥,从Java代码的方式,但是你需要 动态地将其添加到您的布局。
  2. 您不能同时在您的布局中使用该小部件,并使用Java设置API密钥 。

解决方案:

  • 选项1 - 为XML创建一个线性布局,并在添加myMapView 。
  • 选项2 - 使用XML创建地图视图它自己并通过 findViewById获取。
+0

http://stackoverflow.com/questions/3517833/mapactivity-set-apikey-programmatically –

0

尝试将mapview放置在布局xml上,并使用其api键,然后在活动中获取地图的引用并继续正常。

main.xml中使用的MapView:

<com.google.android.maps.MapView android:id="@+id/mapView" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" android:enabled="true" 
    android:layout_centerInParent="true" android:clickable="true" android:apiKey="YOUR MAP KEY"/> 

活动:

​​
0

下面代码为我工作的罚款 -

public class MapTabView extends MapActivity 
{ 

    MapView map; 

    String api = "02gY92RWvQgV-k5CUQvGPowJkw8HkN4Tmm-HDIQ"; 

    @Override 
    protected void onCreate(Bundle icicle) 
    { 
     super.onCreate(icicle); 
     map = new MapView(this, api); 
     map.setClickable(true); 
     map.setEnabled(true); 
     setContentView(map); 
    } 
    @Override 
    protected boolean isRouteDisplayed() { 
     return false; 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.menu, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem menu_item) 
    { 
     switch (menu_item.getItemId()) { 
     case R.id.satelite: 
      map.setSatellite(true); 
      map.setTraffic(false); 
      break; 

     case R.id.traffic: 
      map.setTraffic(true); 
      map.setSatellite(false); 
      break; 

     default: 
      break; 
     } 
     return true; 
    } 
} 

看一看这个TabMapsExample