More and more maps are used in the project. Maps are used everywhere from the O2 O mall, travel, transportation, bicycle and so on. The following are several common function points used in the integration of Golden Map in many projects, as well as filling holes.
Location function
Using the latest Sdk Android_Map3D_SDK_V5.1.0_20170518.jar There are some differences with the previous sdk positioning callbacks.
- Small Blue Point Strategy
The more useful thing to add is LOCATION_TYPE_FOLLOW_NO_CENTER. The position of the small blue dot is controlled by us.myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_SHOW);//Locate only once. myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE) ;//Locate once and move the view to the center of the map. myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW) ;//Continuous positioning, and moving the perspective to the map center, positioning blue dots follow the device movement. (Positioning once a second) myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_MAP_ROTATE);//Continuous positioning, and moving the perspective to the center of the map, the map rotates according to the direction of the device, the positioning point will follow the device to move. (Positioning once a second) myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//Continuous positioning, and moving the perspective to the map center point, the positioning point rotates in accordance with the direction of the device, and will follow the device to move. (Positioning once a second) This mode is executed by default. //The following three modes are available from Version 5.1.0 myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER);//Continuous positioning, blue dots will not move to the center of the map, positioning points rotate according to the direction of the device, and blue dots will follow the device. myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW_NO_CENTER);//Continuous positioning, blue dots will not move to the center of the map, and blue dots will follow the device. myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_MAP_ROTATE_NO_CENTER);//Continuous positioning, blue dots will not move to the center of the map, the map rotates according to the direction of the device, and the blue dots will follow the device.
- Realization of Location
aMap.setOnMyLocationChangeListener(this);
@Override public void onMyLocationChange(Location location) { if (location != null) { Bundle bundle = location.getExtras(); if (bundle != null) { mLocationLatitude = location.getLatitude(); mLocationLongitude = location.getLongitude(); mLocationLatitude = Double.valueOf(df.format(mLocationLatitude)); mLocationLongitude = Double.valueOf(df.format(mLocationLongitude)); if (isFirst) { if (mLocationLatitude > 0 && mLocationLongitude > 0) { CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(new LatLng(mLocationLatitude, mLocationLongitude), 17); aMap.moveCamera(cu); } else { CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(new LatLng(Constant.DEFAULT_LATITUDE, Constant.DEFAULT_LONGITUDE), 17); aMap.moveCamera(cu); } isFirst = false; } } else { ToastUtil.showShort(mContext, "Location failed, please check your location permission"); } } }
Marker
- Points that are always fixed at the center of the screen
private void addMarkerInScreenCenter() { if (screenMarker == null) { screenMarker = aMap.addMarker(new MarkerOptions().zIndex(2) .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_screen_location))); } screenMarker.setAnchor(0.5f, 1.0f); LatLng latLng = aMap.getCameraPosition().target; Point screenPosition = aMap.getProjection().toScreenLocation(latLng); screenMarker.setPositionByPixels(screenPosition.x, screenPosition.y); screenMarker.setClickable(false); }
- Add jump animation to the center point
public void screenMarkerJump(AMap aMap, Marker screenMarker) { if (screenMarker != null) { final LatLng latLng = screenMarker.getPosition(); Point point = aMap.getProjection().toScreenLocation(latLng); point.y -= Utils.dip2px(mContext, 20); LatLng target = aMap.getProjection() .fromScreenLocation(point); //Using Translate Animation, fill in a target point that needs to be moved Animation animation = new TranslateAnimation(target); animation.setInterpolator(new Interpolator() { @Override public float getInterpolation(float input) { // interpolator for simulating gravity acceleration if (input <= 0.5) { return (float) (0.5f - 2 * (0.5 - input) * (0.5 - input)); } else { return (float) (0.5f - Math.sqrt((input - 0.5f) * (1.5f - input))); } } }); //Time required for the entire movement animation.setDuration(600); //Setting up animation screenMarker.setAnimation(animation); //Start animation screenMarker.startAnimation(); } }
- Add multiple Marker Tags
Get the list of Marker s that need to be added and add them to the map in a loop.
The following points should be noted:for (int i = 0; i < mParkingListBeen.size(); i++) { LatLng latLng = new LatLng(mParkingListBeen.get(i).getLatitude(), mParkingListBeen.get(i).getLongitude()); Marker marker = aMap.addMarker(new MarkerOptions() .title("i") .anchor(0.5f, 0.92f) .position(latLng).zIndex(1) .icon(BitmapDescriptorFactory .fromBitmap(mParkingBitmap)) .draggable(false)); marker.setObject(mParkingListBeen.get(i)); mBikeMarkers.add(marker); aMap.setOnMarkerClickListener(this); }
1.Marker's bitmap should be initialized in advance and used when Marker is added. 2.Marker needs to click on the pop-up window and must set the title attribute 3. The default values of anchor are 0.5f and 1.0f, which are offset ratios relative to x and y directions. 4.zIndex Sets the Overlapping Sequence of the z-axis 5.Marker's information can be set Object (), retrieved with getObject() and coerced. 6. Add Marker's Click Events
- Marker's Growth Animation
public void startGrowAnimation(AMap aMap, Marker growMarker) { try { if (growMarker != null) { Animation animation = new ScaleAnimation(0, 1, 0, 1); animation.setInterpolator(new LinearInterpolator()); //Time required for the entire movement animation.setDuration(150); //Setting up animation growMarker.setAnimation(animation); //Start animation growMarker.startAnimation(); } } catch (Exception e) { e.printStackTrace(); } }
Drawing and Cleaning of Navigation Routes
Route navigation in Sdk can indeed be drawn, but if I want to draw a point again on the current page, nothing else works except the aMap.clear() method. This will lead to a problem that the information that has been drawn on the map has been cleared, resulting in poor experience. So there's a second way to plot a route: using Web Services API.
-
Encapsulate the model based on the returned data
public class WalkingPathBean { private String status; private String info; private String infocode; private String count; private RouteBean route; public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } public String getInfocode() { return infocode; } public void setInfocode(String infocode) { this.infocode = infocode; } public String getCount() { return count; } public void setCount(String count) { this.count = count; } public RouteBean getRoute() { return route; } public void setRoute(RouteBean route) { this.route = route; } public static class RouteBean { private String origin; private String destination; private List<PathsBean> paths; public String getOrigin() { return origin; } public void setOrigin(String origin) { this.origin = origin; } public String getDestination() { return destination; } public void setDestination(String destination) { this.destination = destination; } public List<PathsBean> getPaths() { return paths; } public void setPaths(List<PathsBean> paths) { this.paths = paths; } public static class PathsBean { private String distance; private String duration; private List<StepsBean> steps; public String getDistance() { return distance; } public void setDistance(String distance) { this.distance = distance; } public String getDuration() { return duration; } public void setDuration(String duration) { this.duration = duration; } public List<StepsBean> getSteps() { return steps; } public void setSteps(List<StepsBean> steps) { this.steps = steps; } public static class StepsBean { private String polyline; public String getPolyline() { return polyline; } public void setPolyline(String polyline) { this.polyline = polyline; } } } } }
- Loop together into a list of all routes
if (size > 0) { String distance = walkingPathBean.getContent().getRoute().getPaths().get(0).getDistance(); String duration = walkingPathBean.getContent().getRoute().getPaths().get(0).getDuration(); List<LatLng> latLngs = new ArrayList<>(); int steps = walkingPathBean.getContent().getRoute().getPaths().get(0).getSteps().size(); latLngs.add(orignLat); for (int i = 0; i < steps; i++) { String polylines = walkingPathBean.getContent().getRoute().getPaths().get(0).getSteps().get(i).getPolyline(); String[] latlngArr = polylines.split(";"); if (latlngArr.length > 0) { for (String lanLon : latlngArr) { String[] latlng_str = lanLon.split(","); if (latlng_str.length > 1) { LatLng mLatLng = new LatLng(Double.valueOf(latlng_str[1]), Double.valueOf(latlng_str[0])); latLngs.add(mLatLng); } } } } latLngs.add(destLat); }
- Then you can draw it.
polyline = aMap.addPolyline(new PolylineOptions(). addAll(latLngs).width(14f).color(Color.parseColor("#CEE00E"))); zoomToSpan(latLngs);
//Adapt the route to screen zooming according to the position of the screen private void zoomToSpan(List<LatLng> latLngs) { LatLngBounds.Builder b = LatLngBounds.builder(); for (LatLng latLng : latLngs) { b.include(latLng); } LatLngBounds bounds = b.build(); int top_padding = Utils.dip2px(mContext, 96 + 70); int bottom_padding = Utils.dip2px(mContext, 40 + 20 + 10); int left_right_padding = Utils.dip2px(mContext, 15); aMap.moveCamera(CameraUpdateFactory .newLatLngBoundsRect(bounds, left_right_padding, left_right_padding, top_padding, bottom_padding)); }
- Clear and redraw
In this way, we can easily complete the drawing and cleaning of route planning without considering other elements on the map.if (polyline != null) { polyline.remove(); polyline = null; }