下面列出了怎么用com.google.android.gms.maps.model.CameraPosition的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void onLocationChanged(Location location) {
if (location.getLatitude() != 0 || location.getLongitude() != 0) {
setFusedLatitude(location.getLatitude());
setFusedLongitude(location.getLongitude());
stopFusedLocation();
CameraPosition oldPos = googleMap.getCameraPosition();
CameraPosition pos = CameraPosition.builder(oldPos)
.target(getPosition())
.zoom(zoom)
.build();
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(pos));
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(getPosition())
.title("My Location").icon(BitmapDescriptorFactory.fromResource(R.drawable.placeholder)));
AsyncTask placesAsync = new Places().execute(getPosition());
}
}
/**
* Creates a callback for receiving location events.
*/
private void createLocationCallback() {
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
Location location = locationResult.getLastLocation();
if (location != null) {
extractGeocode(location.getLatitude(), location.getLongitude());
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(location.getLatitude(), location.getLongitude()))
.zoom(15)
.build();
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
handleLocationRetrieval(location);
extractGeocode(location.getLatitude(), location.getLongitude());
}
}
};
}
protected void addSingleMarkerToMap(LatLng latLng) {
if (mMap != null) {
if (mCurrentMapMarker != null) {
mCurrentMapMarker.remove();
}
CameraPosition cameraPosition =
new CameraPosition.Builder().target(latLng)
.zoom(getDefaultZoom())
.build();
hasWiderZoom = false;
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
mCurrentMapMarker = addMarker(latLng);
mMap.setOnMarkerDragListener(this);
extractGeocode(latLng.latitude, latLng.longitude);
}
}
void addOriginMarker(LatLng latLng) {
if (mMap != null) {
if (mOriginMapMarker != null) {
mOriginMapMarker.remove();
mOriginMapMarker = null;
}
CameraPosition cameraPosition =
new CameraPosition.Builder().target(latLng)
.zoom(getDefaultZoom())
.build();
hasWiderZoom = false;
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
if (mOriginMarkerPinIconRes != DEFAULT_MARKER_ICON_RES) {
mOriginMapMarker = addMarker(latLng, mOriginMarkerPinIconRes);
} else {
mOriginMapMarker = addMarker(latLng);
}
mMap.setOnMarkerDragListener(this);
showBothMarkersAndGetDirections();
}
}
/**
* Called repeatedly during any animations or gestures on the map (or once, if the camera is
* explicitly set). This may not be called for all intermediate camera positions. It is always
* called for the final position of an animation or gesture.
*
* @param cameraPosition
*/
@Override
public void onCameraChange(CameraPosition cameraPosition) {
WritableMap event = Arguments.createMap();
WritableMap data = Arguments.createMap();
data.putDouble("latitude", cameraPosition.target.latitude);
data.putDouble("longitude", cameraPosition.target.longitude);
data.putDouble("zoom", cameraPosition.zoom);
event.putString("event", "didChangeCameraPosition");
event.putMap("data", data);
reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
mapView.getId(),
"topChange",
event
);
}
@Override
public void onLocationChanged(Location location) {
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(AskLocationActivity.address1.getLatitude(), AskLocationActivity.address1.getLongitude());
mMap.addMarker(new MarkerOptions().position(sydney).title(AskLocationActivity.place1));
// mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
//move camera when location changed
LatLng latLng_Now = new LatLng(location.getLatitude(), location.getLongitude());
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(sydney) // Sets the center of the map to LatLng (refer to previous snippet)
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(45) // Sets the tilt of the camera to 45 degrees
.build(); // Creates a CameraPosition from the builder
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
if (latLng_Prev == null) {
latLng_Prev = latLng_Now;
}
//draw line between two locations:
/* Polyline line = mMap.addPolyline(new PolylineOptions()
.add(latLng_Prev, latLng_Now)
.width(5)
.color(Color.RED));
latLng_Prev = latLng_Now;*/
}
@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.getUiSettings().setMapToolbarEnabled(false);
// Add marker for geocache and move camera
LatLng markerPos = new LatLng(location.getLatitude(), location.getLongitude());
googleMap.addMarker(new MarkerOptions()
.position(markerPos));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(markerPos)
.zoom(13).build();
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
private void moveToSingle(Adapter adapter, int index, boolean animate) {
CameraPosition cp = adapter.getCameraPosition(index);
CameraUpdate cu;
if (cp != null && cp.target != null
&& cp.target.latitude != 0.0
&& cp.target.longitude != 0.0) {
cu = CameraUpdateFactory.newCameraPosition(cp);
if (hidden) showMarkers();
if (markers.get(index) != null) markers.get(index).showInfoWindow();
}
else {
cu = defaultPosition;
hideInfoWindowSingle();
}
if (animate) map.animateCamera(cu);
else map.moveCamera(cu);
}
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;
}
};
}
private void initMap() {
if (map == null) {
SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
if (mapFragment != null) {
map = mapFragment.getMap();
if (map != null) {
UiSettings uiSettings = map.getUiSettings();
uiSettings.setAllGesturesEnabled(false);
uiSettings.setScrollGesturesEnabled(true);
uiSettings.setZoomGesturesEnabled(true);
map.setOnCameraChangeListener(new OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition arg0) {
moveMapCameraToBoundsAndInitClusterkraf();
}
});
}
}
} else {
moveMapCameraToBoundsAndInitClusterkraf();
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition position) {
LatLngBounds bounds = mGoogleMap.getProjection().getVisibleRegion().latLngBounds;
mAdapter.setBounds(bounds);
}
});
}
@Override
protected void onPostExecute(Void aVoid) {
if (status == ConnectionResult.SUCCESS) {
// create marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title(venueAddress);
// Changing marker icon
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(latitude, longitude)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
} else {
DialogFragment dialogFragment = new PlayServicesUnavailableDialogFragment();
dialogFragment.show(getActivity().getFragmentManager(), "Play Service Problem");
}
}
private void setInitialCameraPosition() {
double lng, lat;
float tilt, bearing, zoom;
SharedPreferences settings = getActivity().getSharedPreferences( EXTRAS_SHARED_PREFERENCES, 0 );
lng = Double.longBitsToDouble( settings.getLong( SAVED_STATE_LONG, Double.doubleToLongBits( mLocationClient.getLastLocation().getLongitude() ) ) );
lat = Double.longBitsToDouble( settings.getLong( SAVED_STATE_LAT, Double.doubleToLongBits( mLocationClient.getLastLocation().getLatitude() ) ) );
zoom = settings.getFloat( SAVED_STATE_ZOOM, 17 );
bearing = settings.getFloat( SAVED_STATE_BEARING, 0 );
tilt = settings.getFloat( SAVED_STATE_TILT, 30 );
CameraPosition cameraPosition = new CameraPosition.Builder()
.target( new LatLng( lat, lng) )
.zoom( zoom )
.bearing( bearing )
.tilt( tilt )
.build();
if( cameraPosition == null || mMap == null )
return;
mMap.animateCamera( CameraUpdateFactory.newCameraPosition( cameraPosition ) );
}
@Override
public void onDestroy() {
super.onDestroy();
if( mMap == null || mMap.getCameraPosition() == null )
return;
CameraPosition cam = mMap.getCameraPosition();
SharedPreferences shared = getActivity().getSharedPreferences( EXTRAS_SHARED_PREFERENCES, 0 );
SharedPreferences.Editor editor = shared.edit();
editor.putLong( SAVED_STATE_LAT, Double.doubleToLongBits( cam.target.latitude ) );
editor.putLong( SAVED_STATE_LONG, Double.doubleToLongBits( cam.target.longitude ) );
editor.putFloat( SAVED_STATE_TILT, cam.tilt );
editor.putFloat( SAVED_STATE_BEARING, cam.bearing );
editor.putFloat( SAVED_STATE_ZOOM, cam.zoom );
editor.commit();
}
@Override
public void onChanged(@Nullable Location location) {
if (locationChangedListener != null) {
locationChangedListener.onLocationChanged(location);
}
if (locateState == LocateState.FOLLOW) {
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(location.getLatitude(), location.getLongitude()))
.zoom(17)
.bearing(location.getBearing())
.build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
/**
* Called when the tilt more button (the one with the /) is clicked.
*/
public void onTiltMore(View view) {
if (!checkReady()) {
return;
}
CameraPosition currentCameraPosition = map.getCameraPosition();
float currentTilt = currentCameraPosition.tilt;
float newTilt = currentTilt + 10;
newTilt = (newTilt > 90) ? 90 : newTilt;
CameraPosition cameraPosition = new CameraPosition.Builder(currentCameraPosition)
.tilt(newTilt).build();
changeCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
/**
* Called when the tilt less button (the one with the \) is clicked.
*/
public void onTiltLess(View view) {
if (!checkReady()) {
return;
}
CameraPosition currentCameraPosition = map.getCameraPosition();
float currentTilt = currentCameraPosition.tilt;
float newTilt = currentTilt - 10;
newTilt = (newTilt > 0) ? newTilt : 0;
CameraPosition cameraPosition = new CameraPosition.Builder(currentCameraPosition)
.tilt(newTilt).build();
changeCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = mMapFragment.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
mMap.setMyLocationEnabled(false);
mMap.getUiSettings().setCompassEnabled(false);
mMap.getUiSettings().setZoomControlsEnabled(false);
mMap.getUiSettings().setMyLocationButtonEnabled(false);
LatLng update = getLastKnownLocation();
if (update != null) {
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(CameraPosition.fromLatLngZoom(update, 11.0f)));
}
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
mIsNeedLocationUpdate = false;
moveToLocation(latLng, false);
}
});
}
}
}
@Override
public boolean zoomIn() {
final CameraPosition cameraPosition = mGoogleMap.getCameraPosition();
final float maxZoom = mGoogleMap.getMaxZoomLevel();
if (cameraPosition.zoom < maxZoom) {
mGoogleMap.moveCamera(CameraUpdateFactory.zoomIn());
return true;
}
return false;
}
/**
* Create a new instance with details for a given {@link Gallery}.
*
* @param context The context this runs in.
* @param gallery The gallery of which the details should be displayed.
* @return A newly instantiated fragment.
*/
public static DetailFragment newInstance(@NonNull Context context, @NonNull Gallery gallery,
@NonNull CameraPosition cameraPosition) {
DetailFragment fragment = new DetailFragment();
Bundle args = new Bundle();
args.putParcelable(IntentKeys.GALLERY, gallery);
args.putParcelable(IntentKeys.CAMERA_POSITION, cameraPosition);
fragment.setArguments(args);
final TransitionInflater inflater = TransitionInflater.from(context);
fragment.setSharedElementEnterTransition(
inflater.inflateTransition(R.transition.detail_shared_enter));
fragment.setEnterTransition(new Fade());
return fragment;
}
/**
* Enable updating of detail information.
*
* @param gallery The new gallery to display.
* @param cameraPosition The initial camera position for the details.
*/
public void setNewDetailInformation(@NonNull Gallery gallery,
@NonNull CameraPosition cameraPosition) {
mGallery = gallery;
mCameraPosition = cameraPosition;
loadDetails();
}
private OnCameraPositionUpdateListener getOnCameraPositionUpdateListener() {
if (mCameraListener == null) {
mCameraListener = new OnCameraPositionUpdateListener() {
@Override
public void onCameraPositionUpdate(CameraPosition cameraPosition) {
mCameraPosition = cameraPosition;
}
};
}
return mCameraListener;
}
private void showDetailFragment(@NonNull final GalleryViewHolder holder,
@NonNull final Gallery gallery) {
// Turn of transition grouping for clicked item view to break card structure.
((ViewGroup) holder.itemView).setTransitionGroup(false);
holder.mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
CameraPosition cameraPosition = googleMap.getCameraPosition();
performDetailTransition(holder, gallery, cameraPosition);
}
});
}
private void performDetailTransition(@NonNull GalleryViewHolder itemHolder,
@NonNull Gallery gallery,
CameraPosition cameraPosition) {
DetailFragment fragment = getDetailFragment(gallery, cameraPosition);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
@SuppressLint("CommitTransaction")
FragmentTransaction transaction = fragmentManager.beginTransaction()
.replace(android.R.id.content, fragment, DetailFragment.TAG);
createAndAddTransitionParticipants(itemHolder, transaction);
transaction.addToBackStack(DetailFragment.TAG).commit();
}
@Override
public void onCameraChange(CameraPosition cameraPosition) {
if (mOnCameraPositionUpdateListener != null) {
mOnCameraPositionUpdateListener.onCameraPositionUpdate(cameraPosition);
}
super.onCameraChange(cameraPosition);
}
private void tiltMore()
{
CameraPosition currentCameraPosition = googleMap.getCameraPosition();
float currentTilt = currentCameraPosition.tilt;
float newTilt = currentTilt + 10;
newTilt = (newTilt > 90) ? 90 : newTilt;
CameraPosition cameraPosition = new CameraPosition.Builder(currentCameraPosition).tilt(newTilt).build();
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
private void tiltLess()
{
CameraPosition currentCameraPosition = googleMap.getCameraPosition();
float currentTilt = currentCameraPosition.tilt;
float newTilt = currentTilt - 10;
newTilt = (newTilt > 0) ? newTilt : 0;
CameraPosition cameraPosition = new CameraPosition.Builder(currentCameraPosition).tilt(newTilt).build();
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
private void changeToAmapView() {
if (googlemap != null) {
zoom = googlemap.getCameraPosition().zoom;
latitude = googlemap.getCameraPosition().target.latitude;
longitude = googlemap.getCameraPosition().target.longitude;
}
mapbtn.setText("To 谷歌地图");
mAmapView = new TextureMapView(this, new AMapOptions()
.camera(new com.amap.api.maps.model.CameraPosition(new LatLng(latitude, longitude), zoom, 0, 0)));
mAmapView.onCreate(null);
mAmapView.onResume();
mContainerLayout.addView(mAmapView, mParams);
mGoogleMapView.animate().alpha(0f).setDuration(500).setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mGoogleMapView.setVisibility(View.GONE);
mContainerLayout.removeView(mGoogleMapView);
if (mGoogleMapView != null) {
mGoogleMapView.onDestroy();
}
}
});
//AMap amap = mAmapView.getMap();
amap = mAmapView.getMap();
amap.setOnCameraChangeListener(this);
amap.setOnMapClickListener(this);
mIsAmapDisplay = true;
}
@Override
public void onCameraChange(CameraPosition cp)
{
for (TileOverlayHolder overlay : tileOverlayList) {
if ("points".equals(overlay.getOverlayType().getType())) {
final boolean automaticShowPoints = cp.zoom >= MapProperties.MAP_AUTO_SWITCH_VALUE;
if (automaticShowPoints && !overlay.getTileOverlay().isVisible()) {
overlay.getTileOverlay().setVisible(true);
}
else if (!automaticShowPoints && overlay.getTileOverlay().isVisible()) {
overlay.getTileOverlay().setVisible(false);
}
}
}
}
@Override
public void onCameraChangeFinish(com.amap.api.maps.model.CameraPosition cameraPosition) {
longitude = cameraPosition.target.longitude;
latitude = cameraPosition.target.latitude;
zoom = cameraPosition.zoom;
if (!isInArea(latitude, longitude) && mIsAmapDisplay && mIsAuto) {
changeToGoogleMapView();
}
}