表现:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.yu.lbs" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <uses-library android:name="com.google.android.maps" /> <activity android:name="com.yu.lbs.MainActivity" 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>
main.xml中:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.google.android.maps.MapView android:id="@+id/mapView" android:layout_width="match_parent" android:layout_height="match_parent" android:apiKey=".........." android:clickable="true" android:enabled="true" /> </LinearLayout>
MainActivity.java:
import android.os.Bundle; import android.view.Menu; import com.google.android.maps.MapActivity; public class MainActivity extends MapActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } }
活动可以开始.但是在MapView中什么都没有.可以显示小网格的地图,我认为它带有MapView,但没有加载地图.可能有什么不对?我使用的是Google API v3.但是这段代码来自使用API v1的教科书.
解决方法
我得到解决的方式,我不得不这样做:
final MapView mapView = (MapView)fragmentView.findViewById(R.id.map_fieldLocation); mapView.onCreate(savedInstanceState); mapView.getMapAsync(new OnMapReadyCallback() { @Override public void onMapReady(GoogleMap googleMap) { LatLng coordinates = new LatLng(match.match.LocationLatitude,match.match.LocationLongitude); googleMap.addMarker(new MarkerOptions().position(coordinates).title(match.match.LocationAddress)); googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(coordinates,15)); mapView.onResume(); } });
我缺少的重要部分是你必须在调用getMapAsync()之前调用onCreate()方法,一旦调用了回调,你需要在MapView对象上调用onResume().
这完全解决了我.
这是你自己班里的样子:
public class MainActivity extends MapActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); if (getView() != null) { final MapView mapView = (MapView)getView().findViewById(R.id.mapView); mapView.onCreate(savedInstanceState); mapView.getMapAsync(new OnMapReadyCallback() { @Override public void onMapReady(GoogleMap googleMap) { LatLng coordinates = new LatLng(match.match.LocationLatitude,match.match.LocationLongitude); googleMap.addMarker(new MarkerOptions().position(coordinates).title(match.match.LocationAddress)); googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(coordinates,15)); mapView.onResume(); } } } } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } }
希望这可以帮助!