下面列出了怎么用com.google.android.gms.maps.model.Marker的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public boolean onMarkerClick(Marker marker) {
if (lastSelected != null) {
lastSelected.setSelected(false);
}
final T item = markerCache.get(marker);
if (item != null) {
item.setSelected(true);
lastSelected = item;
}
return onMarkerClickListener != null
&& onMarkerClickListener.onMarkerClick(markerCache.get(marker));
}
@Override
public void refreshMarkerIcons(Filter<Temporal> filter) {
for (MapMarkerObservation mapMarkerObservation : mapObservations.getMarkers()) {
Marker marker = mapMarkerObservation.getMarker();
Observation observation = mapMarkerObservation.getObservation();
if (observation != null) {
if (filter != null && !filter.passesFilter(observation)) {
this.remove(observation);
} else {
boolean showWindow = marker.isInfoWindowShown();
// make sure to set the Anchor after this call as well, because the size of the icon might have changed
marker.setIcon(ObservationBitmapFactory.bitmapDescriptor(context, observation));
marker.setAnchor(0.5f, 1.0f);
if (showWindow) {
marker.showInfoWindow();
}
}
}
}
}
private void addMarker() {
LatLng coordinate = new LatLng(mLatitude, mLongitude);
if (mMap != null) {
MarkerOptions markerOptions;
try {
mMap.clear();
imgSearch.setText("" + userAddress);
markerOptions = new MarkerOptions().position(coordinate).title(userAddress).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_place_red_800_24dp));
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(coordinate, 14);
mMap.animateCamera(cameraUpdate);
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
Marker marker = mMap.addMarker(markerOptions);
marker.showInfoWindow();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
/**
* Highlight the marker by marker.
*/
private void highLightMarker(Marker marker) {
/*
for (Marker foundMarker : this.markers) {
if (!foundMarker.equals(marker)) {
foundMarker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
} else {
foundMarker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
foundMarker.showInfoWindow();
}
}
*/
marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
// marker.showInfoWindow();
//marker.remove();
//Utils.bounceMarker(googleMap, marker);
this.selectedMarker = marker;
}
private void rotateMarket(Marker marker, final float degrees, GoogleMap mMap){
final Handler handler=new Handler();
long start= SystemClock.uptimeMillis();
final float startRotation=currentLocationMarket.getRotation();
final long duration=1500;
final Interpolator interpolator=new LinearInterpolator();
handler.post(new Runnable() {
@Override
public void run() {
long elapsed=SystemClock.uptimeMillis();
float t=interpolator.getInterpolation((float)elapsed/duration);
float rot=t*degrees+(1-t)*startRotation;
currentLocationMarket.setRotation(-rot>180?rot/2:rot);
if (t<1.0){
handler.postDelayed(this, 16);
}
}
});
}
private GoogleMap.OnMarkerClickListener createMarkerClickListenerSingle(final Adapter adapter) {
return new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
for (int i = 0; i < adapter.getCount(); i++) {
CameraPosition cp = adapter.getCameraPosition(i);
if (cp != null && cp.target != null
&& cp.target.latitude == marker.getPosition().latitude
&& cp.target.longitude == marker.getPosition().longitude) {
viewPager.setCurrentItem(i);
marker.showInfoWindow();
return true;
}
}
return false;
}
};
}
public float getAiLineDistance() {
float distance = 0.0f;
if (this.mMarkerList.size() == 2) {
return (float) GeoTools.getDistance(((Marker) this.mMarkerList.get(0)).getPosition(), ((Marker) this.mMarkerList.get(1)).getPosition()).valueInMeters();
}
for (int i = 0; i < this.mMarkerList.size(); i++) {
if (i != 0) {
distance += (float) GeoTools.getDistance(((Marker) this.mMarkerList.get(i - 1)).getPosition(), ((Marker) this.mMarkerList.get(i)).getPosition()).valueInMeters();
}
}
return distance;
}
public List<MapPointLatLng> getInterestPointList() {
List<MapPointLatLng> list = new ArrayList();
for (int i = 0; i < this.interestMarkerList.size(); i++) {
list.add((MapPointLatLng) ((Marker) this.interestMarkerList.get(i)).getTag());
}
return list;
}
private void addConditionMarkersToMap() {
try {
for(MarkerOptions options : liveMarkerOptions) {
Marker marker = mMap.addMarker(options);
}
} catch(ConcurrentModificationException cme) {
log("concurrentmodificationexception adding markers to map " + cme.getMessage());
}
}
@Override
public View getInfoWindow(final Marker marker) {
Pair<Location, User> pair = markerIdToPair.get(marker.getId());
final Location location = pair.first;
final User user = pair.second;
if (user == null || location == null) {
return null;
}
LayoutInflater inflater = LayoutInflater.from(context);
final View v = inflater.inflate(R.layout.people_info_window, null);
TextView name = v.findViewById(R.id.name);
name.setText(user.getDisplayName());
TextView date = v.findViewById(R.id.date);
date.setText(new PrettyTime().format(location.getTimestamp()));
final ImageView avatarView = v.findViewById(R.id.avatarImageView);
Bitmap avatar = avatars.get(user.getId());
if (avatar == null) {
GlideApp.with(context)
.asBitmap()
.load(Avatar.Companion.forUser(user))
.placeholder(R.drawable.ic_person_gray_24dp)
.circleCrop()
.into(getTarget(avatarView, marker, user));
} else {
avatarView.setImageBitmap(avatar);
}
return v;
}
@Override
protected boolean onMarkerClick(Marker marker) {
if (marker.getTag() instanceof String) {
String tag = (String) marker.getTag();
if (mState.newOrders.containsKey(tag)) {
selectOrder(tag);
return true;
}
}
return false;
}
/**
* Add the list of points as markers
*
* @param map google map
* @param points points
* @param customMarkerOptions custom marker options
* @param ignoreIdenticalEnds ignore identical ends flag
* @return list of markers
*/
public List<Marker> addPointsToMapAsMarkers(GoogleMap map,
List<LatLng> points, MarkerOptions customMarkerOptions,
boolean ignoreIdenticalEnds) {
List<Marker> markers = new ArrayList<Marker>();
for (int i = 0; i < points.size(); i++) {
LatLng latLng = points.get(i);
if (points.size() > 1 && i + 1 == points.size() && ignoreIdenticalEnds) {
LatLng firstLatLng = points.get(0);
if (latLng.latitude == firstLatLng.latitude
&& latLng.longitude == firstLatLng.longitude) {
break;
}
}
MarkerOptions markerOptions = new MarkerOptions();
if (customMarkerOptions != null) {
markerOptions.icon(customMarkerOptions.getIcon());
markerOptions.anchor(customMarkerOptions.getAnchorU(),
customMarkerOptions.getAnchorV());
markerOptions.draggable(customMarkerOptions.isDraggable());
markerOptions.visible(customMarkerOptions.isVisible());
markerOptions.zIndex(customMarkerOptions.getZIndex());
}
Marker marker = addLatLngToMap(map, latLng, markerOptions);
markers.add(marker);
}
return markers;
}
public void addInterestByDevice(AckGetAiLinePoint point) {
MapPointLatLng mp = new MapPointLatLng();
mp.isIntertestPoint = true;
mp.isMapPoint = true;
mp.nPos = this.interestMarkerList.size() + 1;
mp.altitude = (float) point.getAltitudePOI();
mp.latitude = point.getLatitudePOI();
mp.longitude = point.getLongitudePOI();
Marker interestMarker = addInterestMarker(new LatLng(mp.latitude, mp.longitude), mp.altitude, mp);
interestMarker.setTag(mp);
interestMarker.setDraggable(true);
this.interestMarkerList.add(interestMarker);
}
/**
* {@inheritDoc}
*/
@Override
public void delete(Marker marker) {
if (markers.remove(marker)) {
marker.remove();
update();
}
}
@Override
public void animateCameraToAllEstablishments(GoogleMap mMap) {
if (mMap != null) {
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker marker : mDeviceMarkerHash.values()) {
builder.include(marker.getPosition());
}
LatLngBounds bounds = builder.build();
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100));
}
}
@Override public void setOnMarkerClickListener(final OnMapMarkerClickListener listener) {
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override public boolean onMarkerClick(Marker marker) {
AirMapMarker<?> airMarker = markers.get(marker);
if (airMarker != null) {
return listener.onMapMarkerClick(airMarker);
}
return false;
}
});
}
@Nullable
@Override
public Establishment getEstablishmentFromMarker(Marker marker) {
for (Map.Entry entry : mDeviceMarkerHash.entrySet()) {
if (marker.equals(entry.getValue())) {
return (Establishment) entry.getKey();
}
}
return null;
}
public void addSmallMakerByMap(Marker marker1, Marker marker2) {
MapPointLatLng mpl2 = (MapPointLatLng) marker2.getTag();
LatLng[] latLng = this.mapCalcAngle.getLineLatLngInterval(marker1.getPosition(), marker2.getPosition(), 3);
float[] angleArray = new float[]{mpl2.showAngle, mpl2.showAngle};
for (int i = 0; i < latLng.length; i++) {
MapPointLatLng mpl = new MapPointLatLng();
mpl.isSelect = true;
mpl.setAngle(angleArray[i]);
Marker mMarker = this.googleMap.addMarker(new MarkerOptions().position(latLng[i]).icon(this.gdCustemMarkerView.createPointWithSmallArrow(this.context, R.drawable.x8_ai_line_point_small1, mpl.showAngle, true)).anchor(0.5f, 0.5f).draggable(false));
mMarker.setTag(mpl);
mMarker.setFlat(true);
this.arrowMarkerList.add(mMarker);
}
}
/**
* Expand the bounding box by the markers
*
* @param boundingBox bounding box
* @param markers list of markers
*/
private void expandBoundingBoxMarkers(BoundingBox boundingBox,
List<Marker> markers) {
for (Marker marker : markers) {
expandBoundingBox(boundingBox, marker.getPosition());
}
}
@Override
protected void onToggleAction() {
if (shouldShow()) {
for (final LatLng latLng : this.vehicleSpawns) {
final MarkerOptions markerOptions = createMarkerOptions();
markerOptions.position(latLng);
this.vehicleMarkers.add(this.mapController.addMarker(markerOptions));
}
} else {
for (final Marker marker : this.vehicleMarkers) {
marker.remove();
}
this.vehicleMarkers.clear();
}
}
/**
* {@inheritDoc}
*/
@Override
public void delete(Marker marker) {
if (markers.remove(marker)) {
marker.remove();
parentPolygon.update();
}
}
@Override
public View getInfoContents(Marker marker) {
if (mOptions.getCheckedRadioButtonId() != R.id.custom_info_contents) {
// This means that the default info contents will be used.
return null;
}
render(marker, mContents);
return mContents;
}
@Override
public void setVisibility(boolean visible) {
if (this.visible == visible)
return;
this.visible = visible;
for (Marker m : locationIdToMarker.values()) {
m.setVisible(visible);
}
}
@Override
public void onInfoWindowLongClick(Marker marker) {
Collection collection = mAllObjects.get(marker);
if (collection != null && collection.mInfoWindowLongClickListener != null) {
collection.mInfoWindowLongClickListener.onInfoWindowLongClick(marker);
}
}
public void removeLinePoint(LatLng homeLocation) {
MapPointLatLng removeMapPointLatLng = null;
for (MapPointLatLng mMapPointLatLng : this.mMapPointList) {
LatLng mLatLng = this.mSelectMarker.getPosition();
if (mLatLng.longitude == mMapPointLatLng.longitude && mLatLng.latitude == mMapPointLatLng.latitude) {
removeMapPointLatLng = mMapPointLatLng;
break;
}
}
if (removeMapPointLatLng != null) {
this.mMapPointList.remove(removeMapPointLatLng);
}
for (Marker marker : this.mMarkerList) {
if (this.mSelectMarker == marker) {
break;
}
}
this.mMarkerList.remove(this.mSelectMarker);
this.mSelectMarker.remove();
int i = 0;
for (Marker marker2 : this.mMarkerList) {
i++;
MapPointLatLng p = (MapPointLatLng) marker2.getTag();
p.nPos = i;
changePointMarker(marker2, p, false);
}
drawPointLine(homeLocation);
this.mSelectMarker = null;
this.lineMarkerSelectListener.onMarkerSizeChange(this.mMarkerList.size());
}
private void addIcon(IconGenerator iconFactory, String text, LatLng position) {
MarkerOptions markerOptions = new MarkerOptions().
icon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon(text))).
position(position).
anchor(iconFactory.getAnchorU(), iconFactory.getAnchorV());
Marker addedTemp = mMap.addMarker(markerOptions);
//log("added temp icon");
}
/**
* @see com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener#
* onInfoWindowClick(com.google.android.gms.maps.model.Marker)
*/
@Override
public void onInfoWindowClick(Marker marker) {
Clusterkraf clusterkraf = clusterkrafRef.get();
if (clusterkraf != null) {
boolean handled = false;
ClusterPoint clusterPoint = clusterkraf.currentClusterPointsByMarker.get(marker);
OnInfoWindowClickDownstreamListener downstreamListener = clusterkraf.options.getOnInfoWindowClickDownstreamListener();
if (downstreamListener != null) {
handled = downstreamListener.onInfoWindowClick(marker, clusterPoint);
}
if (handled == false && clusterPoint != null) {
if (clusterPoint.size() > 1) {
switch(clusterkraf.options.getClusterInfoWindowClickBehavior()) {
case ZOOM_TO_BOUNDS:
clusterkraf.zoomToBounds(clusterPoint);
break;
case HIDE_INFO_WINDOW:
marker.hideInfoWindow();
break;
case NO_OP:
// no-op
break;
}
} else {
switch(clusterkraf.options.getSinglePointInfoWindowClickBehavior()) {
case HIDE_INFO_WINDOW:
marker.hideInfoWindow();
break;
case NO_OP:
// no-op
break;
}
}
}
}
}
@Override
public void refresh(Pair<Location, User> pair) {
// TODO Maybe a different generic for this case
// TODO implementing room might help solve this problem
// In this case I know just the user is coming in
// grab the location based on that
User user = pair.second;
Marker marker = userIdToMarker.get(pair.second.getId());
if (marker != null) {
Location location = markerIdToPair.get(marker.getId()).first;
markerIdToPair.put(marker.getId(), new Pair(location, user));
marker.setIcon(LocationBitmapFactory.bitmapDescriptor(context, location, user));
}
}
@Override
protected void onToggleAction() {
if (shouldShow()) {
for (final LatLng latLng : this.boatSpawns) {
final MarkerOptions markerOptions = createMarkerOptions();
markerOptions.position(latLng);
this.boatMarkers.add(this.mapController.addMarker(markerOptions));
}
} else {
for (final Marker marker : this.boatMarkers) {
marker.remove();
}
this.boatMarkers.clear();
}
}
protected void initializeMap() {
if( mMap == null )
return;
mMap.setTrafficEnabled( true );
mMap.setMapType( GoogleMap.MAP_TYPE_HYBRID );
mMap.setMyLocationEnabled( true );
mMap.setIndoorEnabled( true );
mMap.setMyLocationEnabled( true );
mMap.setOnMapLongClickListener( new GoogleMap.OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng latLng) {
if( mCallback == null )
return;
mCallback.longClickedMap( latLng );
}
});
mMap.setOnMarkerClickListener( new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick( Marker marker ) {
if ( marker == null )
return false;
marker.remove();
if ( markerLocations.contains( marker ) )
markerLocations.remove( marker );
return true;
}
});
}