美文网首页
Android-谷歌地图开发(总结)

Android-谷歌地图开发(总结)

作者: 金色狐狸 | 来源:发表于2017-05-17 18:18 被阅读1078次

    1.添加依赖及注册key
    compile 'com.google.android.gms:play-services-maps:10.2.1'
    compile 'com.google.android.gms:play-services:10.2.1'
    注册key地址: https://console.developers.google.com,选择凭据进行注册,注册完记得在信息中心启用相应的API
    2.在AndroidManifest中添加key
    <meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="@string/google_maps_key" />
    3.初始化显示地图
    在要显示地图的layout中加入
    <fragment
    android:id="@+id/map"
    class="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
    4.在activity中进行初始化
    private GoogleMap mMap;
    private GoogleApiClient mGoogleApiClient;
    private Location mLastLocation;
    private LocationRequest mLocationRequest;// 位置请求
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
    .findFragmentById(R.id.map); //获取map控件id
    mapFragment.getMapAsync(onMapReadyCallback); //设置回调
    mGoogleApiClient = new GoogleApiClient.Builder(this) //初始化GoogleApiClient
    .addConnectionCallbacks(connectedListener) //添加连接回调监听
    .addOnConnectionFailedListener(connectionFailedListener) //添加连接失败回调监听
    .addApi(LocationServices.API) //添加需要使用到的API
    .addApi(Places.PLACE_DETECTION_API)
    .addApi(Places.GEO_DATA_API)
    .build();
    5.地图设置参考,记得在onMapReadyCallback里进行设置,地图没准备好,会出现异常。

    QQ20170517-082532.png

    6.地图加载完成的回调
    protected OnMapReadyCallback onMapReadyCallback = new OnMapReadyCallback() {
    @Override
    public void onMapReady(GoogleMap googleMap) {
    mGoogleMap = googleMap;
    mapSetting(); //第5步的mapsetting

    //设置地图开始移动方法
    mGoogleMap.setOnCameraMoveStartedListener(new GoogleMap.OnCameraMoveStartedListener() {
    @Override
    public void onCameraMoveStarted(int i) {

            });
    

    //设置地图位置发生改变方法
    mGoogleMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
    @Override
    public void onCameraIdle() {
    LatLng lat = mGoogleMap.getCameraPosition().target;//获取当前屏幕中点坐标
    }
    });
    }
    };
    7.客户端连接回调
    private GoogleApiClient.ConnectionCallbacks connectedListener = new GoogleApiClient.ConnectionCallbacks() {
    @Override
    public void onConnectionSuspended(int arg0) {
    ViseLog.i("onConnectionSuspended");
    }

        @Override
        public void onConnected(Bundle arg0) {
            // 请求最后已知的位置
            mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        }
    };
    private GoogleApiClient.OnConnectionFailedListener connectionFailedListener = new GoogleApiClient.OnConnectionFailedListener() {
        @Override
        public void onConnectionFailed(@NonNull ConnectionResult arg0) {
        }
    };
    

    8.位置监听
    // 位置监听
    private LocationListener locationListener = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
    // 第一次定位时,将地图位置移动到当前位置
    if (isFristLocation) {
    isFristLocation = false;
    mLastLocation = location;
    double startLatitude1 = mLastLocation.getLatitude();
    double startLongitude1 = mLastLocation.getLongitude();
    mGoogleMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(startLatitude1, startLongitude1)));
    stopLocationUpdates();
    }
    }
    };
    //开始定位
    protected void startLocationUpdates() {
    if (mGoogleApiClient.isConnected()) {
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, locationListener);
    }
    }
    //停止定位
    protected void stopLocationUpdates() {
    if (mGoogleApiClient.isConnected()) {
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, locationListener);
    }
    }
    9.生命周期
    @Override
    protected void onStart() {
    // 开启图层定位
    if (!mGoogleApiClient.isConnecting()) {
    mGoogleApiClient.connect();
    }
    super.onStart();
    }

    @Override
    protected void onStop() {
        // 关闭图层定位
        super.onStop();
        mGoogleApiClient.disconnect();
    }
    

    10.添加marker
    mGoogleMap.addMarker(new MarkerOptions().position(marker的坐标).icon(BitmapDescriptorFactory.fromResource(marker图片资源)));
    11.移动到指定坐标
    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(你要的坐标, 地图放大比例));
    mGoogleMap.animateCamera(CameraUpdateFactory.newLatLng(你要的坐标));
    12.画线方法
    PolylineOptions polylineOptions = new PolylineOptions().addAll(坐标集合).width(10).color(Color.BLUE);
    Polyline line0 = mGoogleMap.addPolyline(polylineOptions);
    13.其他参考资料
    https://developers.google.com/maps/?hl=zh-cn

    相关文章

      网友评论

          本文标题:Android-谷歌地图开发(总结)

          本文链接:https://www.haomeiwen.com/subject/kizpottx.html