下面是代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.googlemaptest"
    android:versionCode="1"
    android:versionName="1.0" >    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.googlemaptest.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>
    <meta-data
 android:name="com.google.android.maps.v2.API_KEY"
     android:value="要自己输的"/>
    <permission
          android:name="com.example.googlemaptest.permission.MAPS_RECEIVE"
          android:protectionLevel="signature"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<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"/>   
</manifest>googlemap.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:map="http://schemas.android.com/apk/res-auto"
   android:id="@+id/mapView"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   class="com.google.android.gms.maps.MapFragment"
   map:cameraBearing="45"
   map:cameraTargetLat="25.033611"
   map:cameraTargetLng="121.565000"
   map:cameraTilt="0"
   map:cameraZoom="13"
   map:uiCompass="true"
   map:mapType="normal"
   map:uiRotateGestures="true"
   map:uiScrollGestures="true"
   map:uiTiltGestures="true"
   map:uiZoomControls="false"
   map:uiZoomGestures="true" />activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mapContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="定位"
            android:textSize="15sp" />        <LinearLayout
            android:id="@+id/location"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="horizontal" >            <LinearLayout
                android:id="@+id/mapsChoice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >                <!-- 定义选择地图类型的单选按钮 -->                <RadioGroup
                    android:id="@+id/rg_mapType"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="horizontal" >                    <RadioButton
                        android:id="@+id/rb_nomal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:checked="true"
                        android:text="普通视图" />                    <RadioButton
                        android:id="@+id/rb_satellite"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="卫星视图" />
                </RadioGroup>
            </LinearLayout>
        </LinearLayout>        <include
            android:id="@+id/googleMap"
            android:layout_weight="127.57"
            layout="@layout/googlemap" />
    </LinearLayout>
 </LinearLayout>
地图

解决方案 »

  1.   

    MainActivity.java
    package com.example.googlemaptest;
    import com.google.android.gms.maps.CameraUpdateFactory;
    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.MapFragment;
    import com.google.android.gms.maps.model.BitmapDescriptorFactory;
    import com.google.android.gms.maps.model.CameraPosition;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.MarkerOptions;
    import android.util.Log;
    import android.view.Menu;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Context;
    import android.widget.RadioGroup;
    import android.widget.RadioGroup.OnCheckedChangeListener;
    public class MainActivity extends Activity {
    public static final String LOG_TAG = "HelloGPS";
        LocationManager locationManager = null;
        String provider = null;
        //定义界面上的可视化组件
        private RadioGroup rg_mapType;
        GoogleMap mMap;
        private CameraPosition cameraPosition;
        private MarkerOptions erOpt;
        //定义LocationManager对象
        private LocationManager locManager;
        private Location location;
        private String bestProvider;
    @Override
        protected void onCreate(Bundle savedInstanceState) {
    Log.v(LOG_TAG, "onCreate");
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         //获取用户界面的组件
         rg_mapType = (RadioGroup) findViewById(R.id.rg_mapType);
         //GPS定位
            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            provider = LocationManager.GPS_PROVIDER;
            location = locationManager.getLastKnownLocation(provider);
            updateToNewLocation(location);
         //取得地图组件
         mMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.mapView)).getMap();
         mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
         updateToNewLocation(location);
         //为RadioGroup的选中状态改变添加监听器
         rg_mapType.setOnCheckedChangeListener(new ChangeMapTypeListener());
         // 设置监听器,自动更新的最小时间为间隔N秒(1秒为1*1000,这样写主要为了方便)或最小位移变化超过N
         locManager.requestLocationUpdates(bestProvider,  3 * 1000, 8, new LocationListener() {
         //当Provider的状态改变时
         @Override
         public void onStatusChanged(String provider, int status, Bundle extras) {
         }
         @Override
         public void onProviderEnabled(String provider) {
         // 当GPS LocationProvider可用时,更新位置
         }
         @Override
         public void onProviderDisabled(String provider) {
         updateToNewLocation(null);
         }
         @Override
         public void onLocationChanged(Location location) {
         // 当GPS定位信息发生改变时,更新位置
         updateToNewLocation(location);
         }
         });
        }
        @Override
        protected void onResume()
        {
            Log.v(LOG_TAG, "onResume");
            super.onResume();        locationManager.requestLocationUpdates(provider, 0, 0,
                    mLocationListener);
        }
        @Override
        protected void onPause()
        {
            Log.v(LOG_TAG, "onPause");
            super.onPause();        locationManager.removeUpdates(mLocationListener);
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu)
        {
            Log.v(LOG_TAG, "onCreateOptionsMenu");
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
        /**
         * 新位置更新
         * 
         * @param location
         */
        private class ChangeMapTypeListener implements OnCheckedChangeListener{
         @Override
         public void onCheckedChanged(RadioGroup group, int checkedId) {
         switch(checkedId){
         case R.id.rb_nomal://如果勾选的是"正常视图"的单选按钮
         mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
         break;
         case R.id.rb_satellite://如果勾选的是"卫星视图"的单选按钮
         mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
         break;
         }
         }
        }
        private void updateToNewLocation(Location location){
         mMap.clear();
         erOpt = new MarkerOptions();
         //定位
         double dLong = 120.038062;
         double dLat = 30.233822;
         if(location != null){
         //获取经度
         dLong = location.getLongitude();
         //获取纬度
         dLat = location.getLatitude();
         }
         erOpt.position(new LatLng(dLat, dLong));
         erOpt.draggable(false);
         erOpt.visible(true);
         erOpt.anchor(0.5f, 0.5f);//设为图片中心
         erOpt.icon(BitmapDescriptorFactory
         .fromResource(android.R.drawable.ic_menu_mylocation));
         mMap.addMarker(erOpt);
         //将摄影机移动到指定的地理位置
         cameraPosition = new CameraPosition.Builder()
         .target(new LatLng(dLat, dLong))              // Sets the center of the map to ZINTUN
         .zoom(17)                   // 缩放比例
         .bearing(0)                // Sets the orientation of the camera to east
         .tilt(30)                   // Sets the tilt of the camera to 30 degrees
            .build();                   // Creates a CameraPosition from the builder
         mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
        }
        LocationListener mLocationListener = new LocationListener()
        {        @Override
            public void onStatusChanged(String provider, int status, Bundle extras)
            {
                Log.v(LOG_TAG, "onStatusChanged");
            }        @Override
            public void onProviderEnabled(String provider)
            {
                Log.v(LOG_TAG, "onProviderEnabled");        }        @Override
            public void onProviderDisabled(String provider)
            {
                Log.v(LOG_TAG, "onProviderDisabled");        }        @Override
            public void onLocationChanged(Location location)
            {
                Log.v(LOG_TAG, "onLocationChanged ++" + location.toString());
                updateToNewLocation(location);
            }
        };
    }
      

  2.   

    打断点调试,加log,看logcat这个调试技能楼主需要掌握
      

  3.   

    闪退就是出错了,并且这个错误没有被处理。
    把手机连接到电脑,开启调试横暴工,就会在开发环境的 log 视图中显示错误信息。
      

  4.   

    就是logcat不大会看啊像这种闪退或者FC的问题就抓到logcat,然后搜索Fatal或者Exception等字眼,一般就可以找到问题所在了