I'm trying to get the distance between two UIImageView on my screen, and for that I planned on substracting the center coordinates of the image 2 (movable) from the image 1 (not movable).
For that, I need to be able to get dynamically the coordinates of these images and I'm using something like this :
var centerBoardX = CGRectGetMidX(BlackBoard.frame)
var centerBoardY = CGRectGetMidY(BlackBoard.frame)
var centerRoundX = CGRectGetMidX(round1.frame)
var centerRoundY = CGRectGetMidY(round1.frame)
println(centerBoardX - centerRoundX)
println(centerBoardY - centerRoundY)
But even if I'm moving the image2 all over the screen I always get the same result based on the initial coordinates of both UIImageViews.
What's wrong ? Why do I get only the initial coordinates and not the new ones?
Thanks !
OK, so I found what was not right.
My UIImageViews are defined by frames, and I was getting the coordinates of the frame, which are always the same.
To get the correct coordinates, I needed to get the info from the superview of the object.
So the working code is
var centerBoardX = BlackBoard.superview?.center.x
var centerBoardY = BlackBoard.superview?.center.y
var centerRoundX = Round1.superview?.center.x
var centerRoundY = Round1.superview?.center.y
println(centerBoardX! - centerRoundX!)
println(centerBoardY! - centerRoundY!)
And now it's perfectly working !
Related
I'm working on an iOS app with a MapBox map.
I'm displaying MGLAnnotations but I only want to create the annotations that can be seen on the screen at a current moment.
Problem is : I have a UIView on the bottom of my screen so the bounds I get with mapView.visibleCoordinateBounds function is not really accurate because I can't see the bottom of my map.
I can't just resize my map because the bottom view I talked about doesn't cover the entire map, we still see parts of the map behind the bottom view.
So my question is, how can I get the visibleCoordinateBounds for a CGRect over my MapView ?
My current solution works only for the entire map view bounds and not the bounds of the CGRect area I want to display the annotations :
if ((mapView.visibleCoordinateBounds.sw).latitude...(mapView.visibleCoordinateBounds.ne).latitude ~= latX && (mapView.visibleCoordinateBounds.sw).longitude...(mapView.visibleCoordinateBounds.ne).longitude ~= lngY) {
print("VISIBLE")
}
Thanks
I finally found a way to do it.
I just calculate the percentage of my mapview hidden by the bottom view and I minus this number to the south latitude :
var valueToRemoveBottom = (mapView.visibleCoordinateBounds.ne).latitude - (mapView.visibleCoordinateBounds.sw).latitude
let percentageUsedByBottom = (bottomView.frame.height * 100 / mapView.frame.height) / 100
valueToRemoveBottom = valueToRemoveBottom * Double(percentageUsedByBottom)
if (((mapView.visibleCoordinateBounds.sw).latitude + valueToRemoveBottom)...((mapView.visibleCoordinateBounds.ne).latitude) ~= latX && (mapView.visibleCoordinateBounds.sw).longitude...(mapView.visibleCoordinateBounds.ne).longitude ~= lngY) {
// visible
}
I have searched and found code that first inserts an image into a Table in a Google Doc.The image however is larger than the desired image space--and so we have to resize the picture by first getting the dimensions of the inserted picture and then reinserting the picture into the table. The code seems to work well except I am left with two pictures the original sized picture and the scaled picture in the specified table location.
My challenge is that when I attempt to delete the first picture after getting the sizing information needed to scale the picture -- nothing ends up being saved. Here is the code:
if(celltext === "%PIC%") {
table.replaceText("%PIC%", "");
table.getCell(row, cell).insertImage(0, image);
sizePic(table,image, row,cell);
}
function sizePic(table, image) {
var cellImage = table.getCell(row, cell).insertImage(0, image);
//get the dimensions of the image AFTER having inserted it to fix
//its dimensions afterwards
var originW = cellImage.getWidth();
var originH = cellImage.getHeight();
var newW = originW;
var newH = originH;
var ratio = originW/origin
var styleImage = {};
var maxWidth = 200;
if(originW>maxWidth){
newW = maxWidth;
newH = parseInt(newW/ratio);
table.getCell(row,cell).clear();
}
cellImage.setWidth(newW).setHeight(newH).setAttributes(styleImage);
}
}
The problematic line is the table.getCell(row,cell).clear(); that even though it occurs after the image is inserted and before the sized image is inserted, it doesn't appear to work that way. Please note that my code is the result of looking at an existing post How to resize image on Google app Script.
I'm trying to detect all the layers seen in the view of the map (OpenLayers 3).
I've tried this method but it works just for a pixel.
map.forEachLayerAtPixel(evt.pixel, function(layer){
// And I edit the layer...
});
Is there any function that allow me to do it?
thanks.
You should be able to loop through the layers and check if the extent intersects with the view extent. That will at least get you the layers that have some pixels within the current view.
var viewExtent = map.getView().calculateExtent(map.getSize());
var layersInView = [];
map.getLayers().forEach(function (layer) {
var layerExtent = layer.getExtent();
if (ol.extent.intersects(layerExtent, viewExtent)) {
layersInView.push(layer);
}
});
I'm not sure how you'd tell if a layer is actually visible to the user, but this might get you closer.
I've tried everything I can find suggested elsewhere and I've also tried every permutation of the code you can see below and I just cannot crack this.
The plot itself is working great there isn't an issue there. I have a certain screen in my application that I want to draw an OxyPlot onto this view but rotate 90 degrees to suit the data better (for various reasons the application is currently locked to portrait).
The code in the view is:
private void CreatePlotChart()
{
var normalRect = new CGRect(0,0, View.Frame.Width, View.Frame.Height);
var rotatedRect = new CGRect(0, 0, View.Frame.Height, View.Frame.Width); // height and width swapped
var plot = new PlotView();
var radians = -90d.ToRadians();
plot.Transform = CGAffineTransform.MakeRotation((nfloat)radians);
// plot.Frame = normalRect;
plot.Model = ViewModel.Model;
// plot.InvalidatePlot(true); // no discernable effect
Add(plot);
// plot.Draw(rotatedRect); // context error
View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
View.AddConstraints(
plot.AtTopOf(View),
plot.AtLeftOf(View),
plot.WithSameHeight(View),
plot.WithSameWidth(View),
plot.AtBottomOf(View)
);
}
The code above results in a chart as shown below, I also get this exact same chart if I pass in the rotatedRect to the constructor new PlotView(rotatedRect):
If I remove the use of constraints and pass in the rotatedRect like this:
private void CreatePlotChart()
{
var normalRect = new CGRect(0,0, View.Frame.Width, View.Frame.Height);
var rotatedRect = new CGRect(0, 0, View.Frame.Height, View.Frame.Width); // height and width swapped
var plot = new PlotView(rotatedRect);
var radians = -90d.ToRadians();
plot.Transform = CGAffineTransform.MakeRotation((nfloat)radians);
// plot.Frame = normalRect;
plot.Model = ViewModel.Model;
// plot.InvalidatePlot(true); // no discernable effect
Add(plot);
// plot.Draw(rotatedRect); // context error
// View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
//View.AddConstraints(
// plot.AtTopOf(View),
// plot.AtLeftOf(View),
// plot.WithSameHeight(View),
// plot.WithSameWidth(View),
// plot.AtBottomOf(View)
// );
}
I get a lot closer to the desired effect, as can be seen below:
If I go another step and "reset" it's frame to the cached normalRect I get even closer, with this:
All of these attempts feel too hacky. What is the best way of achieving the chart manipulation I need and maintaining the use of constraints to make sure things are positioned properly?
Update
If after the first chart is drawn and I kick off another select of data, the exact same code rendered the chart exactly correctly:
This is also 100% repeatable so I think this might be a bug in OxyPlot or some odd side effect of the way I'm using it.
the method getRadius() of class ol.geom.Circle returns the radius of a circle ;
how can I convert this value into meters ?
I draw a circle on a map using a particular projection (e.g Spherical Mercator, LambertIIe, etc.)
then I convert this geometry into degree to process further treatments such as testing if a point (in degrees) is inside this circle
so getting the radius always in the same unit (meter) from a geometry in degree would be useful
thanks in advance
Jean-Marie
You can use the ol.proj.METERS_PER_UNIT table to get the radius in meters:
var units = map.getView().getProjection().getUnits();
var radiusM = circle.getRadius() * ol.proj.METERS_PER_UNIT[units];
I use the following method to get the radius (getFirstCoordinate referring to the center and getLastCoordinate referring to a point on the perimeter) :
var wgs84Sphere = new ol.Sphere(6378137);
var center=circle.getFirstCoordinate();
var ray=getDistance(center[0],center[1],circle.getLastCoordinate()[0],circle.getLastCoordinate()[1]);
// getDistance returns the distance between 2 points (source : http://openlayers.org/en/v3.7.0/examples/measure.html)
function getDistance(long1,lat1,long2,lat2) {
var c1 = [long1,lat1];
var c2 = [long2,lat2];
return wgs84Sphere.haversineDistance(c1,c2);
}