How to remove zoom from mapView if user zoom its on button click.
for (id annotation in annotations)
{
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect zoomRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.0, 0.0);
[m_mapView setVisibleMapRect:zoomRect animated:YES];
}
MKMapRectMake cant have 0,0 in rect's width height. once u can make it to a value your crashing will stop
Related
I am using react with typescript. I have a canvas and I am trying to draw a rectangle inside the canvas based on mouseup and mousedown event. I don't know what I am doing wrong here my rectangles are drawn but in different positions.
This is how my code looks like:
const Home = () => {
const divRef = useRef<HTMLCanvasElement>(null);
useEffect(() => {
let paint = false;
let StartClientXCord: any;
let StartClientYCord: any;
let FinishedClientXCord: any;
let FinishedClientYCord: any;
function startPosition(e:any){
paint = true;
StartClientXCord = e.clientX;
StartClientYCord = e.clientY;
}
function finishPosition(e:any){
FinishedClientXCord = e.clientX;
FinishedClientYCord = e.clientY;
paint = false;
}
function draw(e: any){
if(!paint){
return
}
if(divRef.current){
const canvas = divRef.current.getContext('2d');
if(canvas){
canvas?.rect(StartClientXCord, StartClientYCord, FinishedClientXCord - StartClientXCord, FinishedClientYCord - StartClientYCord);
canvas.fillStyle = 'rgba(100,100,100,0.5)';
canvas?.fill();
canvas.strokeStyle = "#ddeadd";
canvas.lineWidth = 1;
canvas?.stroke();
}
}
}
if(divRef.current){
divRef.current.addEventListener("mousedown", startPosition);
divRef.current.addEventListener("mouseup", finishPosition);
divRef.current.addEventListener("mousemove", draw);
}
})
return (
<canvas className='canvas' style={{height: height, width: width}} ref={divRef}>
</canvas>
)
}
I am drawing on a different position box is drawing on a different position. Here I attached the screenshot as well
I believe it's because you are using clientX and clientY, which MDN defines as:
The clientX read-only property of the MouseEvent interface provides the horizontal coordinate within the application's viewport at which the event occurred (as opposed to the coordinate within the page).
This is the absolute position on the entire page. What you want is the is the position reative to the top left corner of the canvas. You want offsetX and offsetY which MDN defines as:
The offsetX read-only property of the MouseEvent interface provides the offset in the X coordinate of the mouse pointer between that event and the padding edge of the target node.
So give this a try:
StartClientXCord = e.offsetX;
StartClientYCord = e.offsetY;
I have clicked on a coordinates and remove with the help of MouseEvent clientY and ClientX value. I have some coordinates in database and I want to hit them same as it clicked as event. I have used bellow code.
const pixel = map.getPixelFromCoordinate(coordinates);
event = {}
event.clientX = pixel[0];
event.clientY = pixel[1];
simulateSingleClick(map, event);
But not able to click because event's clientY and above clientY value has lot of different.
The map viewport is a viewport within the client viewport https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect so unless you have a full page map you will need to add the left and top offsets of that
const vpClient = map.getViewport().getBoundingClientRect();
const pixel = map.getPixelFromCoordinate(coordinates);
event = {}
event.clientX = vpClient.left + pixel[0];
event.clientY = vpClient.top + pixel[1];
simulateSingleClick(map, event);
I put my search bar in the navigation bar with these code:
UISearchBar* searchBar = [[UISearchBar alloc] init];
searchBar.delegate = self;
[searchBar setShowsSearchResultsButton:YES];
self.navigationItem.titleView = searchBar;
When I run the same code in iOS 7, it works as I want.
But the search bar cannot work in iOS 6: when I touch it, the keyboard doesnot appear; when I click the result list button in the search bar, nothing happens, too.
Help!
(The base SDK is iOS 7.0)
Edit1
Solving the problem like this:
Just cover the search bar on the navigation bar.
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)
// Left Cancel button.
UIBarButtonItem *barbtnCancel = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancel:)];
self.navigationItem.leftBarButtonItem = barbtnCancel;
// Right Search button.
UIBarButtonItem *barbtnSearch = [[UIBarButtonItem alloc] initWithTitle:#"Search" style:UIBarButtonItemStyleBordered target:self action:#selector(search:)];
self.navigationItem.rightBarButtonItem = barbtnSearch;
// Navigation bar.
UINavigationBar* navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
[navBar pushNavigationItem:self.navigationItem animated:YES];
[self.view addSubview:navBar];
// Search bar.
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(53, 0, SCREEN_WIDTH-53-53, 44)];
self.searchBar.delegate = self;
[self.searchBar setBarStyle:UIBarStyleDefault];
[self.searchBar setKeyboardType:UIKeyboardTypeDefault];
[self.searchBar setPlaceholder:#"Search Something"];
[self.searchBar setShowsSearchResultsButton:NO];
[self.view addSubview:self.searchBar];
}
You need to initialize the search bar with a frame that is not CGRectZero.
I am trying to overlay a play button on an image thumbnail
currentl the thumbnail is displayed using :
if(nil != self.analysis.image) {
self.imageView.image = self.analysis.image;
}
i tried to use the thumbnail as the background image , and the play button with transparency as the image, but it doesn't appear at all ...
if(nil != self.analysis.image) {
self.imageView.backgroundColor = [UIColor colorWithPatternImage:self.analysis.image];
self.imageView.image = [UIImage imageNamed:#"whiteBackground.png"];
}
What's the best way to compose them ?
My fault !
The coding is right...!! whiteBackground.png is transparent ....
I have to use the display button image ....
if(nil != self.analysis.image) {
self.imageView.backgroundColor = [UIColor colorWithPatternImage:self.analysis.image];
self.imageView.image = [UIImage imageNamed:#"displayButton.png"];
}
I have a table with a big list of stuff that comes from a plist file and clicking each of them takes you to a new view, a xib.
I have 2 views inside that .xib, one for portrait and one for landscape
In my h file I have this:
IBOutlet UIView *portraitView;
IBOutlet UIView *landscapeView;
#property (nonatomic, retain) IBOutlet UIView *portraitView;
#property (nonatomic, retain) IBOutlet UIView *landscapeView;
In my m file this:
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:#"UIDeviceOrientationDidChangeNotification" object:nil];
}
- (void) orientationChanged:(id)object
{
UIInterfaceOrientation interfaceOrientation = [[object object] orientation];
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
self.view = self.portraitView;
}
else
{
self.view = self.landscapeView;
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) {
self.view = portraitView;
} else if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
self.view = landscapeView;
}
return YES;
}
#end
Everything was working perfectly in iOS 5, showing landscape or portrait when needed.
Now with the iOS 6 update everything is a mess.
If I am in the table (portrait) view and click one item, it shows correct in portrait, if I rotate to landscape, the view shows the correct view as well, BUT being in landscape, if I go back to the table and select another item, it shows the portrait view instead of the landscape.
If I do the same but starting landscape, it shows portrait.
So, now the orientation is not working for anything.
The same happens to my other views using storyboard. They are portrait and always showed like that, now they rotate, shrink everything and leave my app as trash.
1- How can I fix the .xib orientation thing ?
2- How can I fix the storyboard orientation ? (they were static, now everything rotates (no code at all))
Thanks.
I think that I have a work around. It's ugly but it's working...
With iOS6 Apple suggest now to use 2 differences XIB file to switch between portrait & landscape view.
But if you want to use the previous method allowed in iOS 5.0 by "switching" between 2 UIView IBOutlet, you can try my ugly working solution. The idea is to rotate the view according to the orientation.
1) In you viewDidLoad, subscribe to orientation notification:
- (void)viewDidLoad
{
[super viewDidLoad];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:#"UIDeviceOrientationDidChangeNotification" object:nil];
}
2) Add a method called by the notification:
-(void)orientationChanged:(NSNotification *)object{
NSLog(#"orientation change");
UIDeviceOrientation deviceOrientation = [[object object] orientation];
if(deviceOrientation == UIInterfaceOrientationPortrait || deviceOrientation == UIInterfaceOrientationPortraitUpsideDown){
self.view = self.potraitView;
if(deviceOrientation ==UIInterfaceOrientationPortraitUpsideDown){
NSLog(#"Changed Orientation To PortraitUpsideDown");
[self portraitUpsideDownOrientation];
}else{
NSLog(#"Changed Orientation To Portrait");
[self portraitOrientation];
}
}else{
self.view = self.landscapeView;
if(deviceOrientation ==UIInterfaceOrientationLandscapeLeft){
NSLog(#"Changed Orientation To Landscape left");
[self landscapeLeftOrientation];
}else{
NSLog(#"Changed Orientation To Landscape right");
[self landscapeRightOrientation];
}
}
}
3) And finally, add rotation method for each orientation:
-(void)landscapeLeftOrientation{
// Rotates the view.
CGAffineTransform transform = CGAffineTransformMakeRotation(-(3.14159/2));
self.view.transform = transform;
// Repositions and resizes the view.
CGRect contentRect = CGRectMake(0, 0, 480, 320);
self.view.bounds = contentRect;
}
-(void)landscapeRightOrientation{
// Rotates the view.
CGAffineTransform transform = CGAffineTransformMakeRotation(3.14159/2);
self.view.transform = transform;
// Repositions and resizes the view.
CGRect contentRect = CGRectMake(0, 0, 480, 320);
self.view.bounds = contentRect;
}
-(void)portraitOrientation{
// Rotates the view.
CGAffineTransform transform = CGAffineTransformMakeRotation(0);
self.view.transform = transform;
// Repositions and resizes the view.
CGRect contentRect = CGRectMake(0, 0, 320, 480);
self.view.bounds = contentRect;
}
-(void)portraitUpsideDownOrientation{
// Rotates the view.
CGAffineTransform transform = CGAffineTransformMakeRotation(3.14159);
self.view.transform = transform;
// Repositions and resizes the view.
CGRect contentRect = CGRectMake(0, 0, 320, 480);
self.view.bounds = contentRect;
}
I suggest you to make a custom UIViewController class and inherit-ate from this class to save redundant code.
If you want to support both solution for ios5 and ios6 you can use a #endif macro to include the both code in your controllers.
Cheers
No need to send and receive notifications:
In your appdelegate.m the following method
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
is always called to check the window's orientation,
so a simple way around is to have the below described code in your appdelegate.m
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
NSUInteger orientations = UIInterfaceOrientationMaskPortrait;
if(self.window.rootViewController){
UIViewController *presentedViewController ;
if ([self.window.rootViewController isKindOfClass:([UINavigationController class])])
{
presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
}
else if ([self.window.rootViewController isKindOfClass:[UITabBarController class]]){
UITabBarController *controller = (UITabBarController*)self.window.rootViewController;
id selectedController = [controller presentedViewController];
if (!selectedController) {
selectedController = [controller selectedViewController];
}
if ([selectedController isKindOfClass:([UINavigationController class])])
{
presentedViewController = [[(UINavigationController *)selectedController viewControllers] lastObject];
}
else{
presentedViewController = selectedController;
}
}
else
{
presentedViewController = self.window.rootViewController;
}
if ([presentedViewController respondsToSelector:#selector(supportedInterfaceOrientations)]) {
orientations = [presentedViewController supportedInterfaceOrientations];
}
}
return orientations;
}
and implement
- (NSUInteger)supportedInterfaceOrientations
in the respective view controllers
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait; //Or anyother orientation of your choice
}
and to perform sudden action against orientation changes, implement the following method
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
This is very late for an answer, still I thought I should share this with you just in case,
I had the very same issue.
shouldAutorotateToInterfaceOrientation is deprecated iOS 6 onwards.
You need to parallel this method with the new supportedInterfaceOrientations and shouldAutorotate methods.
And it is very very important, you need to make sure that you set the root controller in your app delegate's applicationDidFinishLaunching method rather than simply adding the view controller's view ( or navigation Controller or tabBar Controller depending on what you are using ) as a subview to the window.