我想在Android中显示谷歌地图,我有地图API甚至地图没有显示,也没有抛出任何错误。 我使用以下 Java代码
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>
最后我输出空白。请看下面的屏幕
请告诉我我哪里错了
答案 0 :(得分:0)
我认为您还没有在当前布局中添加myMapView ...........
您在java代码中创建
this.myMapView = new MapView(this,
"0jzIB6m5R1kLa_rGte-DS9PhF3KlSgqYHUZognA");
但看起来没有在任何布局中添加..根据link
解决方案:
答案 1 :(得分:0)
尝试将mapview放在布局xml上,并在其中放置api键,然后在活动上获取地图的参考并继续正常。
带有mapView的main.xml:
<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"/>
的活动:
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView)findViewById(R.id.mapView);
答案 2 :(得分: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