Select to view content in your preferred language

getting ags point from opengl

2579
19
Jump to solution
08-14-2012 09:49 AM
HamzaHaroon
Regular Contributor
I need to get the ags point on the map (point with long and lat) on a opengl view. right now i have it to getting the user touch on the screen, which is x and y. how can this be done if my glview controlling class is seperate from the mapview controller?

this is how i get input
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { CGRect bounds = [self bounds];     UITouch*    touch = [[event touchesForView:self] anyObject]; firstTouch = YES; // Convert touch point from UIView referential to OpenGL one (upside- down flip) location = [touch locationInView:self]; location.y = bounds.size.height - location.y; } // Handles the continuation of a touch. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {        CGRect bounds = [self bounds]; UITouch* touch = [[event touchesForView:self] anyObject];      // Convert touch point from UIView referential to OpenGL one (upside- down flip) if (firstTouch) { firstTouch = NO; previousLocation = [touch previousLocationInView:self]; previousLocation.y = bounds.size.height - previousLocation.y; } else { location = [touch locationInView:self];     location.y = bounds.size.height - location.y; previousLocation = [touch previousLocationInView:self]; previousLocation.y = bounds.size.height - previousLocation.y; }      // Render the stroke [self renderLineFromPoint:previousLocation toPoint:location]; } // Handles the end of a touch event when the touch is a tap. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { CGRect bounds = [self bounds];     UITouch*    touch = [[event touchesForView:self] anyObject]; if (firstTouch) { firstTouch = NO; previousLocation = [touch previousLocationInView:self]; previousLocation.y = bounds.size.height - previousLocation.y; [self renderLineFromPoint:previousLocation toPoint:location]; } }


here is the code i have for drawing based on user touch
// Drawings a line onscreen based on where the user touches - (void) renderLineFromPoint:(CGPoint)start toPoint:(CGPoint)end { static GLfloat*     vertexBuffer = NULL; static NSUInteger   vertexMax = 64; NSUInteger vertexCount = 0,     count,     i; [EAGLContext setCurrentContext:context]; glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer); // Convert locations from Points to Pixels CGFloat scale = self.contentScaleFactor; start.x *= scale;PaintingView.m 12-08-07 9:34 AM start.y *= scale; end.x *= scale; end.y *= scale; // Allocate vertex array buffer if(vertexBuffer == NULL) vertexBuffer = malloc(vertexMax * 2 * sizeof(GLfloat)); // Add points to the buffer so there are drawing points every X pixels count = MAX(ceilf(sqrtf((end.x - start.x) * (end.x - start.x) + (end.y -  start.y) * (end.y - start.y)) / kBrushPixelStep), 1); for(i = 0; i < count; ++i) { if(vertexCount == vertexMax) { vertexMax = 2 * vertexMax; vertexBuffer = realloc(vertexBuffer, vertexMax * 2 * sizeof (GLfloat)); } vertexBuffer[2 * vertexCount + 0] = start.x + (end.x - start.x) *  ((GLfloat)i / (GLfloat)count); vertexBuffer[2 * vertexCount + 1] = start.y + (end.y - start.y) *  ((GLfloat)i / (GLfloat)count); vertexCount += 1; } // Render the vertex array glVertexPointer(2, GL_FLOAT, 0, vertexBuffer); glDrawArrays(GL_POINTS, 0, vertexCount); // Display the buffer glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); [context presentRenderbuffer:GL_RENDERBUFFER_OES]; }
0 Kudos
19 Replies
HamzaHaroon
Regular Contributor
i made this method in my paintingView class

-(id) initWithPainter:(AGSMapView*)mapView
{
_mapView = mapView;
}

and i call it in my view controller with this line in the viewDidLoad method.

self.paintingView = [[PaintingView alloc] initWithPainter:self.mapView];

in my initWithPainter method, the mapview is initialized, but not anywhere else that i use it. so in the getMapPointFromTouch method, the mapView is null.


my original initWithCoder has bugs, and causes the app to crash.
0 Kudos
NimeshJarecha
Esri Regular Contributor
in my initWithPainter method, the mapview is initialized, but not anywhere else that i use it. so in the getMapPointFromTouch method, the mapView is null.


Because you are not retaining it. In your PaintingView create a *retain* property for mapView and access it by using self.mapView everywhere you are using it.

Regards,
Nimesh
0 Kudos
HamzaHaroon
Regular Contributor
still not working. in my paintingView hearder file

@interface ...
AGSMapView* _mapView;
...
@property (nonatomic, retain) AGSMapView *mapView;
...

in the paintingView class

@synthesize mapview= _mapview

initWithPainter: (AGSMapView*) mapView{
self.mapView = mapView;
}

using self.mapview everwhere else in the paintingView class. in the get touchmethod, i print the mapView to nslog, and it shows null.
0 Kudos
NimeshJarecha
Esri Regular Contributor
Do this...

initWithPainter: (AGSMapView*) mapView{
NSLog(@"%@",mapView);
self.mapView = mapView;
NSLog(@"%@",self.mapView);
}

What is the result?

Regards,
Nimesh
0 Kudos
HamzaHaroon
Regular Contributor
result is the same for both outputs.

<AGSMapView: 0xa987e00; frame = (0 44; 1024, 704); clipsToBounds = YES; autoresize = RM+BM; autoresizeSubviews = NO; layer = <CALayer: 0x9aa0fe0>>
0 Kudos
NimeshJarecha
Esri Regular Contributor
Then self.mapView in all places should give the same result in the same class unless you are explicitly releasing it or your class is getting dealloc.

In other places you are trying to access it as _mapView or self.mapView?

Regards,
Nimesh
0 Kudos
HamzaHaroon
Regular Contributor
accessing as self.mapView. i think my class might be getting dealloc'd somewhere, checking my code for this class right now.

edit: dealloc is only in dealloc method
-(void) dealloc{
   if (brushTexture){
      glDeleteTextures(1, &brushTexture);
      brushTexture = 0; }
   if ([EAGLContext currentContext] == context)
       [EAGLContext setCurrentContext:nil];
   [context release];
   [super dealloc];
}
0 Kudos
NimeshJarecha
Esri Regular Contributor
You'll have to debug your code and find out where mapView is getting dealloc.

Regards,
Nimesh
0 Kudos
HamzaHaroon
Regular Contributor
i figure out why it wasn't working. my uiview that is attached to the PaintingView class is initialized in the storyboard, and is never sent the mapview.

so when i create the object out of this line of code, the mapview is only existing in that object.
self.paintingView = [[PaintingView alloc] initWithPainter:self.mapView];



is there any alternate way to do this?
0 Kudos
HamzaHaroon
Regular Contributor
solved this by making my painting class have a delegate method, and adding the delegate as a protocol into my main view controller.

the delegate method gets the mapview from the main view controller.
0 Kudos