Android 集成GoogleMap,实现定位和获取位置信息

1.准备

我使用的是AS2.2.2,首先FQ注册google开发者帐号,准备获取API Key,网上有许多相关资料我就不再赘述,这里讲一个比较小白级的获取方法,可以减少许多输入
1.1. AS创建项目添加一个Google Map Activity
 
1.2 创建成功后找到google_maps_api.xml,便可看到下图内容,根据我初中英文水平,复制这行到浏览器地址栏,跟着它走就行了,记得FQ
1.3 最后你会看到已经帮你创建好了密钥,复制到项目中
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">YOUR_KEY_HERE</string>
替代即可.
 
1.4 在项目中添加Google Services依赖,我这里选择的是在build.gradle中添加地图服务和位置信息服务
compile 'com.google.android.gms:play-services:9.8.0' 
compile 'com.google.android.gms:play-services-location:9.8.0'

2.代码部分

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap map) {
        // Add a marker in Sydney, Australia, and move the camera.
        LatLng sydney = new LatLng(-34, 151);
        map.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        map.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}
在创建好的MapsActivity中,已经有以上代码,此时运行项目已经可以看到地图界面并定位到了悉尼
现在添加地图定位层,由于地图准备就绪后会调用onMapReady(),我们就在准备就绪后显示定位层
 /**
     * 如果取得了权限,显示地图定位层
     */
    private void enableMyLocation() {
        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            // Permission to access the location is missing.
            PermissionUtils.requestPermission(this, LOCATION_PERMISSION_REQUEST_CODE,
                    android.Manifest.permission.ACCESS_FINE_LOCATION, true);
        } else if (mMap != null) {
            // Access to the location has been granted to the app.
            mMap.setMyLocationEnabled(true);
        }
    }
那么,显示出定位层后,如何获取到位置信息呢,在GoogleMap这个类中没法获取到信息的,得通过Google Play services location APIs 来获取.
这里我推荐一份中文的安卓文档:http://hukai.me/android-training-course-in-chinese/location/index.html  ,有需要的可以了解到更多关于谷歌位置信息服务知识
我们使用APIs中的fused location provider来获取设备的最后可知位置,用getLastLocation()方法为设备的位置构造一个单一请求
onConnected()方法会在Google API Client准备好时调用,可以在这里取得地理位置的经度和纬度坐标
public class MainActivity extends ActionBarActivity implements
        ConnectionCallbacks, OnConnectionFailedListener {
    ...
    @Override
    public void onConnected(Bundle connectionHint) {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation != null) {
            mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
            mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
        }
    }
}
然而有经纬度是不够的,还需要将经纬度转成地理列表,这里使用Geocoder 类的 getFromLocation() 方法接收一个经度和纬度,返回一个地址列表,不过这个操作可能会耗时,所以启动一个IntentService 处理,这里定义一个继承 IntentService 的类 FetchAddressIntentService,这个类是地址查找服务.这个类代码随后在demo中给,你也可以参考api摸索.
这里讲讲如何启动FetchAddressIntentService
public class MainActivity extends ActionBarActivity implements
        ConnectionCallbacks, OnConnectionFailedListener {

    protected Location mLastLocation;
    private AddressResultReceiver mResultReceiver;
    ...

    protected void startIntentService() {
        Intent intent = new Intent(this, FetchAddressIntentService.class);
        intent.putExtra(Constants.RECEIVER, mResultReceiver);     //所需参数一,接收处理结果
        intent.putExtra(Constants.LOCATION_DATA_EXTRA, mLastLocation);    //所需参数二
        startService(intent);
    }
}

必须在 Google Play services 连接稳定之后启动 intent 服务,所以会在刚刚的onConnected()调用startIntentService()

public class MainActivity extends ActionBarActivity implements
        ConnectionCallbacks, OnConnectionFailedListener {
    ...
    @Override
    public void onConnected(Bundle connectionHint) {
        // Gets the best and most recent location currently available,
        // which may be null in rare cases when a location is not available.
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(    //所需参数二

                mGoogleApiClient);

        if (mLastLocation != null) {
            // Determine whether a Geocoder is available.
            if (!Geocoder.isPresent()) {
                Toast.makeText(this, R.string.no_geocoder_available,
                        Toast.LENGTH_LONG).show();
                return;
            }

            if (mAddressRequested) {
                startIntentService();
            }
        }
    }
}
还缺少参数一,这里通过重写 onReceiveResult() 方法来处理发送给接收端的结果

 

class AddressResultReceiver extends ResultReceiver {
        public AddressResultReceiver(Handler handler) {
            super(handler);
        }

        @Override
        protected void onReceiveResult(int resultCode, Bundle resultData) {

            // Display the address string
            // or an error message sent from the intent service.
            mAddressOutput = resultData.getString(Constants.RESULT_DATA_KEY);
            displayAddressOutput();

            // Show a toast message if an address was found.
            if (resultCode == Constants.SUCCESS_RESULT) {
                showToast(getString(R.string.address_found));
            }

        }
    }

实例化就能使用了

  mResultReceiver = new AddressResultReceiver(new Handler());
结束,第一次写,求轻喷,附上Demo
https://github.com/Linyaodai/MyLocationDemo

 

posted @ 2017-10-12 19:32  星辰之力  阅读(9425)  评论(0编辑  收藏  举报