1

我正在使用google map版本 2。我拿到API钥匙,把它们放进去manifest。我可以在某些设备上查看地图,但是当我在不同的设备上编译相同的应用程序时,我会得到空白屏幕来显示地图。任何人都可以指导我吗

    FragmentManager myFragmentManager = getFragmentManager();
    MapFragment myMapFragment = (MapFragment)myFragmentManager.findFragmentById(R.id.map);
    myMap = myMapFragment.getMap();

     if (myMap == null) {
         myMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                                .getMap();
     }
    myMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
    myMap.setMyLocationEnabled(true);
    myMap.setOnMyLocationChangeListener(this);
4

1 回答 1

0

activity_map.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

MapActivity.java

public class MapActivity extends Activity {

private GoogleMap map;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);

    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();

    //modify below according to your requirement
    map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
    map.setMyLocationEnabled(true);
    ..
    ..
    ..

Add following in your manifest file:

<permission
    android:name="com.yourpackage.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

  <uses-permission android:name="com.yourpackage.permission.MAPS_RECEIVE" />

 <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" />

  <uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

   <application
       ........ >
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="your_api_key" />

Of course, you have to other manifest parameter like activity name and other.

Then you have to reference google play service library into your project.
Follow with this link to install google play service sdk Install the Google Play Services SDK

And then

Your Project--->Right Click-->Properites-->Android-->Add

Select google-play-services sdk that you import into your workspace using that i mention in above method

Finally, select your android sdk target to

Google API Version 8

or other Google API version according to your requirement.

Further if you have any confusion, you can follow with this android official link:Google Maps Android API v2

UPDATED CODE:

To make it work in lower version:

activity_map.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      class="com.google.android.gms.maps.SupportMapFragment"/>

MyMapActivity.java

FragmentManager fragmentManager = getSupportFragmentManager();
SupportMapFragment mapFragment =  (SupportMapFragment)
fragmentManager.findFragmentById(R.id.map);
map = mapFragment.getMap();

Thanks !!

于 2013-06-20T08:28:42.033 回答