Map version 2 in Android 2.3

2019-08-07 15:35发布

问题:

I have developed simple Map application using google map version2. It is working fine on Android 4.0 supported devices but not run in Android 2.3(API 10). Now I need to support the same application from API level 10. How to convert the application support from API level 10?

My application is supported the "google-play-services_lib".

my code sample.

main.xml:

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

java code:

    public class MainActivity extends Activity implements LocationListener{
        private GoogleMap mMap;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
           ........
       }
}

please help me to support from API 10(Android 2.3).

回答1:

You mention API 10

You should use SupportMapFragment instead of MapFragment.

<fragment
class="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"  
android:layout_width="match_parent"
android:layout_height="match_parent"/>

You activity must also extend FragmentActivity.

SupportMapFragment fm = (SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.map);
mMap = fm.getMap(); 

Import

import android.support.v4.app.FragmentActivity;  
import com.google.android.gms.maps.SupportMapFragment; 

https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapFragment

Check the line above the topic Developer's guide



回答2:

change to

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

and

extends FragmentActivity

and

mMap =((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();mMap