@Override public void updateLocationMarker(Location location) { if (locationChangedListener != null) { currentUserLocation = location; locationChangedListener.onLocationChanged(location); } // Update clickable area LatLng userPosition = getUserLocation(location); if (userPositionClickArea == null) { MarkerOptions markerOptions = new MarkerOptions(); markerOptions.position(userPosition); markerOptions.anchor(0.4f, 0.4f); // strange google maps bug markerOptions.icon(BitmapDescriptorFactory.fromBitmap(clickableBitmap)); userPositionClickArea = googleMap.addMarker(markerOptions); } else { userPositionClickArea.setPosition(userPosition); } if (userPositionClickArea2 == null) { MarkerOptions markerOptions = new MarkerOptions(); markerOptions.position(userPosition); markerOptions.anchor(0.6f, 0.6f); // strange google maps bug markerOptions.icon(BitmapDescriptorFactory.fromBitmap(clickableBitmap)); userPositionClickArea2 = googleMap.addMarker(markerOptions); } else { userPositionClickArea2.setPosition(userPosition); } }
private MarkerOptions createMarker(ReadableMap markerJson) { MarkerOptions options = new MarkerOptions(); options.position( new LatLng( markerJson.getMap("coordinates").getDouble("lat"), markerJson.getMap("coordinates").getDouble("lng"))); if (markerJson.hasKey("title")) { options.title(markerJson.getString("title")); } if (markerJson.hasKey("color")) { options.icon(BitmapDescriptorFactory.defaultMarker((float) markerJson.getDouble("color"))); } if (markerJson.hasKey("snippet")) { options.snippet(markerJson.getString("snippet")); } if (markerJson.hasKey("icon")) { String varName = ""; ReadableType iconType = markerJson.getType("icon"); if (iconType.compareTo(ReadableType.Map) >= 0) { ReadableMap icon = markerJson.getMap("icon"); try { int resId = getResourceDrawableId(icon.getString("uri")); Bitmap image = BitmapFactory.decodeResource(reactContext.getResources(), resId); options.icon( BitmapDescriptorFactory.fromBitmap( Bitmap.createScaledBitmap( image, icon.getInt("width"), icon.getInt("height"), true))); } catch (Exception e) { varName = icon.getString("uri"); } } else if (iconType.compareTo(ReadableType.String) >= 0) { varName = markerJson.getString("icon"); } if (!varName.equals("")) { // Changing marker icon to use resource int resourceValue = getResourceDrawableId(varName); Log.i(REACT_CLASS, varName + markerJson.toString()); options.icon(BitmapDescriptorFactory.fromResource(resourceValue)); } } if (markerJson.hasKey("anchor")) { ReadableArray anchor = markerJson.getArray("anchor"); options.anchor((float) anchor.getDouble(0), (float) anchor.getDouble(1)); } return options; }
public HostRenderer() { super(getApplicationContext(), mMap, mClusterManager); View sameLocationMultiHostClusterView = getLayoutInflater().inflate(R.layout.same_location_cluster_marker, null); View singleHostMarkerView = getLayoutInflater().inflate(R.layout.location_marker, null); mSingleLocationClusterIconGenerator.setContentView(sameLocationMultiHostClusterView); mSingleLocationClusterIconGenerator.setBackground(null); mSingleHostIconGenerator.setContentView(singleHostMarkerView); mSingleHostIconGenerator.setBackground(null); mSingleHostBitmapDescriptor = BitmapDescriptorFactory.fromBitmap(mSingleHostIconGenerator.makeIcon()); }
@Override protected void onBeforeClusterRendered( Cluster<HostBriefInfo> cluster, MarkerOptions markerOptions) { if (clusterLocationStatus(cluster) == ClusterStatus.all) { int size = cluster.getSize(); BitmapDescriptor descriptor = mIcons.get(size); if (descriptor == null) { // Cache new bitmaps descriptor = BitmapDescriptorFactory.fromBitmap( mSingleLocationClusterIconGenerator.makeIcon(String.valueOf(size))); mIcons.put(size, descriptor); } markerOptions.icon(descriptor); } else { super.onBeforeClusterRendered(cluster, markerOptions); } }
public class LocationActivity extends BaseActivity implements OnGetGeoCoderResultListener { public static final int SEND = 0; public static final String TYPE = "type"; public static final String TYPE_SELECT = "select"; public static final String LATITUDE = "latitude"; public static final String LONGITUDE = "longitude"; public static final String TYPE_SCAN = "scan"; public static final String ADDRESS = "address"; private static BDLocation lastLocation = null; private LocationClient locClient; private MyLocationListener myListener = new MyLocationListener(); private MapView mapView; private BaiduMap baiduMap; private BaiduReceiver receiver; private GeoCoder geoCoder = null; private BitmapDescriptor descriptor = BitmapDescriptorFactory.fromResource(R.drawable.chat_location_activity_icon_geo); private String intentType; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.chat_location_activity); initBaiduMap(); } private void initBaiduMap() { mapView = (MapView) findViewById(R.id.bmapView); baiduMap = mapView.getMap(); baiduMap.setMaxAndMinZoomLevel(18, 13); IntentFilter iFilter = new IntentFilter(); iFilter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR); iFilter.addAction(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR); receiver = new BaiduReceiver(); registerReceiver(receiver, iFilter); geoCoder = GeoCoder.newInstance(); geoCoder.setOnGetGeoCodeResultListener(this); Intent intent = getIntent(); intentType = intent.getStringExtra(TYPE); initActionBar(R.string.chat_position); if (intentType.equals(TYPE_SELECT)) { // 选择发送位置 // 开启定位图层 baiduMap.setMyLocationEnabled(true); baiduMap.setMyLocationConfigeration( new MyLocationConfigeration(MyLocationConfigeration.LocationMode.NORMAL, true, null)); // 定位初始化 locClient = new LocationClient(this); locClient.registerLocationListener(myListener); LocationClientOption option = new LocationClientOption(); option.setProdName("avosim"); option.setOpenGps(true); option.setCoorType("bd09ll"); option.setScanSpan(1000); option.setOpenGps(true); option.setIsNeedAddress(true); option.setIgnoreKillProcess(true); locClient.setLocOption(option); locClient.start(); if (locClient != null && locClient.isStarted()) { locClient.requestLocation(); } if (lastLocation != null) { // 显示在地图上 LatLng ll = new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude()); MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll); baiduMap.animateMapStatus(u); } } else { Bundle b = intent.getExtras(); LatLng latlng = new LatLng(b.getDouble(LATITUDE), b.getDouble(LONGITUDE)); // 维度在前,经度在后 baiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(latlng)); OverlayOptions ooA = new MarkerOptions().position(latlng).icon(descriptor).zIndex(9); baiduMap.addOverlay(ooA); } } @Override public boolean onCreateOptionsMenu(Menu menu) { if (intentType != null && intentType.equals(TYPE_SELECT)) { MenuItem add = menu.add(0, SEND, 0, R.string.chat_location_activity_send); alwaysShowMenuItem(add); } return super.onCreateOptionsMenu(menu); } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { int id = item.getItemId(); if (id == SEND) { gotoChatPage(); } return super.onMenuItemSelected(featureId, item); } private void gotoChatPage() { if (lastLocation != null) { Intent intent = new Intent(); intent.putExtra(LATITUDE, lastLocation.getLatitude()); // 经度 intent.putExtra(LONGITUDE, lastLocation.getLongitude()); // 维度 intent.putExtra(ADDRESS, lastLocation.getAddrStr()); setResult(RESULT_OK, intent); this.finish(); } else { toast(R.string.chat_getGeoInfoFailed); } } @Override public void onGetGeoCodeResult(GeoCodeResult arg0) { // TODO Auto-generated method stub } @Override public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) { // TODO Auto-generated method stub if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) { toast(getString(R.string.chat_cannotFindResult)); return; } Logger.d(getString(R.string.chat_reverseGeoCodeResultIs) + result.getAddress()); lastLocation.setAddrStr(result.getAddress()); } @Override protected void onPause() { mapView.onPause(); super.onPause(); lastLocation = null; } @Override protected void onResume() { mapView.onResume(); super.onResume(); } @Override protected void onDestroy() { if (locClient != null && locClient.isStarted()) { // 退出时销毁定位 locClient.stop(); } // 关闭定位图层 baiduMap.setMyLocationEnabled(false); mapView.onDestroy(); mapView = null; // 取消监听 SDK 广播 unregisterReceiver(receiver); super.onDestroy(); // 回收 bitmap 资源 descriptor.recycle(); } public static void startToSeeLocationDetail(Context ctx, double latitude, double longitude) { Intent intent = new Intent(ctx, LocationActivity.class); intent.putExtra(LocationActivity.TYPE, LocationActivity.TYPE_SCAN); intent.putExtra(LocationActivity.LATITUDE, latitude); intent.putExtra(LocationActivity.LONGITUDE, longitude); ctx.startActivity(intent); } public static void startToSelectLocationForResult(Activity from, int requestCode) { Intent intent = new Intent(from, LocationActivity.class); intent.putExtra(LocationActivity.TYPE, LocationActivity.TYPE_SELECT); from.startActivityForResult(intent, requestCode); } /** 定位SDK监听函数 */ public class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { // map view 销毁后不在处理新接收的位置 if (location == null || mapView == null) return; if (lastLocation != null) { if (lastLocation.getLatitude() == location.getLatitude() && lastLocation.getLongitude() == location.getLongitude()) { Logger.d(getString(R.string.chat_geoIsSame)); // 若两次请求获取到的地理位置坐标是相同的,则不再定位 locClient.stop(); return; } } lastLocation = location; Logger.d( "lontitude = " + location.getLongitude() + ",latitude = " + location.getLatitude() + "," + getString(R.string.chat_position) + " = " + lastLocation.getAddrStr()); MyLocationData locData = new MyLocationData.Builder() .accuracy(location.getRadius()) // 此处设置开发者获取到的方向信息,顺时针0-360 .direction(100) .latitude(location.getLatitude()) .longitude(location.getLongitude()) .build(); baiduMap.setMyLocationData(locData); LatLng ll = new LatLng(location.getLatitude(), location.getLongitude()); String address = location.getAddrStr(); if (address != null && !address.equals("")) { lastLocation.setAddrStr(address); } else { // 反Geo搜索 geoCoder.reverseGeoCode(new ReverseGeoCodeOption().location(ll)); } // 显示在地图上 MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll); baiduMap.animateMapStatus(u); // 设置按钮可点击 } } /** 构造广播监听类,监听 SDK key 验证以及网络异常广播 */ public class BaiduReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { String s = intent.getAction(); if (s.equals(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR)) { toast(getString(R.string.chat_location_mapKeyErrorTips)); } else if (s.equals(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR)) { toast(getString(R.string.chat_pleaseCheckNetwork)); } } } }
public void addMarkers(List<GridPoint> gridPointList, List<PointEntity> pointEntityList) { clearMap(); Marker m; LatLngBounds.Builder builder = new LatLngBounds.Builder(); for (GridPoint gridPoint : gridPointList) { Bitmap icon = gridPoint.getScore() > 10 ? gridPoint.getScore() > 20 ? BitmapFactory.decodeResource(getResources(), R.drawable.red_dot) : BitmapFactory.decodeResource(getResources(), R.drawable.yellow_dot) : BitmapFactory.decodeResource(getResources(), R.drawable.green_dot); float hue = gridPoint.getScore() > 10 ? gridPoint.getScore() > 20 ? BitmapDescriptorFactory.HUE_RED : BitmapDescriptorFactory.HUE_YELLOW : BitmapDescriptorFactory.HUE_GREEN; markerOptions = new MarkerOptions(); markerOptions.visible(true); markerOptions.position( new LatLng( gridPoint.getLocation().getLatitude().doubleValue(), gridPoint.getLocation().getLongitude().doubleValue())); markerOptions.draggable(false); markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon)); // markerOptions.icon(BitmapDescriptorFactory.defaultMarker(hue)); m = googleMap.addMarker(markerOptions); gridMap.put(m.getId(), gridPoint); builder.include(m.getPosition()); } for (PointEntity pointEntity : pointEntityList) { Bitmap icon = pointEntity.getPointType().equals(PointType.PERSON) ? BitmapFactory.decodeResource(getResources(), R.drawable.man) : pointEntity.getPointType().equals(PointType.POLICE_STATION) ? BitmapFactory.decodeResource(getResources(), R.drawable.police) : BitmapFactory.decodeResource(getResources(), R.drawable.hospital); markerOptions = new MarkerOptions(); markerOptions.visible(true); markerOptions.position( new LatLng( pointEntity.getLocation().getLatitude().doubleValue(), pointEntity.getLocation().getLongitude().doubleValue())); markerOptions.draggable(false); markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon)); m = googleMap.addMarker(markerOptions); pointMap.put(m.getId(), pointEntity); builder.include(m.getPosition()); } if (gridPointList.size() > 1) { LatLngBounds bounds = builder.build(); CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 0); googleMap.animateCamera(cu); } else if (gridPointList.size() > 0) { CameraPosition cameraPosition = new CameraPosition.Builder() .target( new LatLng( gridPointList.get(0).getLocation().getLatitude().doubleValue(), gridPointList.get(0).getLocation().getLongitude().doubleValue())) .zoom(zoomLevel) .bearing(0) .tilt(45) .build(); googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); } markersDisplayed = true; heatmapDisplayed = false; clusterDisplayed = false; }