Android Google地图那些事

导入包

implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'com.google.android.gms:play-services-basement:17.1.1'
implementation 'com.google.android.gms:play-services-base:17.1.0'
implementation 'com.google.android.gms:play-services-gcm:17.0.0'
implementation 'com.google.android.gms:play-services-location:17.0.0'
implementation 'com.google.android.libraries.places:places:2.1.0'

AndroidManifest.xml 的application添加

 

密钥在这里申请

布局文件中使用

 

Java中使用

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
if (mapFragment != null) {
    mapFragment.getMapAsync(this);
}

当前实现OnMapReadyCallback,并需要实现onMapReady方法

@Override
public void onMapReady(GoogleMap googleMap) {
    this.googleMap = googleMap;
}

画标注、画轨迹

public void setTrackView(List details) {
    PolylineOptions options = new PolylineOptions();
    options.color(getResources().getColor(R.color.colorAccent));
    options.width(getResources().getDimension(R.dimen.track_link_line_width));
    LatLngBounds.Builder boundsBuilder = LatLngBounds.builder();
    for (SportSceneRunningDetailModel.Detail detail : details) {
        com.google.android.gms.maps.model.LatLng latLng = new com.google.android.gms.maps.model.LatLng(detail.getLatitude(), detail.getLongitude());
        options.add(latLng);
        boundsBuilder.include(latLng);
    }
    googleMap.addPolyline(options);
    // 绘制起点
    BitmapDescriptor bitmap = com.google.android.gms.maps.model.BitmapDescriptorFactory.fromResource(R.mipmap.ic_track_start);
    com.google.android.gms.maps.model.LatLng latLng = new com.google.android.gms.maps.model.LatLng(details.get(0).getLatitude(), details.get(0).getLongitude());
    googleMap.addMarker(new com.google.android.gms.maps.model.MarkerOptions().position(latLng).icon(bitmap).anchor(0.5f, 0.5f));
    // 绘制终点
    bitmap = BitmapDescriptorFactory.fromResource(R.mipmap.ic_track_end);
    latLng = new LatLng(details.get(details.size() - 1).getLatitude(), details.get(details.size() - 1).getLongitude());
    googleMap.addMarker(new MarkerOptions().position(latLng).icon(bitmap).anchor(0.5f, 0.5f));
    // 会根据所以标注和轨迹自动绽放地图大小,达到所有点和线都在屏幕中显示
    CameraUpdate mCameraUpdate = CameraUpdateFactory.newLatLngBounds(boundsBuilder.build(), 100);
    googleMap.moveCamera(mCameraUpdate);
}

SportSceneRunningDetailModel.Detail是项目中的实体,根据自己项目换成经纬度列表就行。

利用原生方式定位

LocationManager mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 执行下面代码前要判断一下ACCESS_FINE_LOCATION和ACCESS_COARSE_LOCATION权限
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 1, this);

当前实现LocationListener接口,下面需要实现的方法,定位成功后会调用onLocationChanged方法。

@Override
public void onLocationChanged(Location location) {
    LogUtil.d("定位成功:" + location.getLatitude() + "," + location.getLongitude());
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

你可能感兴趣的:(android,map,google地图,定位)