注释图钉是可拖动的,但我无法将其拖放到目标位置。问题是我怎样才能得到我放置注释图钉的目标位置的坐标?有什么方法吗?
编辑 1:
注释图钉的中心似乎从未改变过我放置图钉的位置。
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState
fromOldState:(MKAnnotationViewDragState)oldState
{
if (newState == MKAnnotationViewDragStateEnding)
{
NSLog(@"x is %f", annotationView.center.x);
NSLog(@"y is %f", annotationView.center.y);
CGPoint dropPoint = CGPointMake(annotationView.center.x, annotationView.center.y);
CLLocationCoordinate2D newCoordinate = [self.mapView convertPoint:dropPoint toCoordinateFromView:annotationView.superview];
[annotationView.annotation setCoordinate:newCoordinate];
}
}
编辑 2:
注释.h
@property (nonatomic,readwrite,assign) CLLocationCoordinate2D coordinate;
注释.m
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate {
coordinate = newCoordinate;
}
编辑 3: 无论我在哪里拖动图钉,droppedAt 的 NSLog 输出总是相同的。
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState
fromOldState:(MKAnnotationViewDragState)oldState
{
if (newState == MKAnnotationViewDragStateEnding)
{
CLLocationCoordinate2D droppedAt = annotationView.annotation.coordinate;
NSLog(@"Pin dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);
}
}
编辑 4:
注释.m @动态启动地址;
- (CLLocationCoordinate2D)coordinate
{
coordinate.latitude = [self.startAddr.latitude doubleValue];
coordinate.longitude = [self.startAddr.longitude doubleValue];
return coordinate;
}
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate {
//I need to update the self.startAddr.latitude and longitude here. Problem solved.
self.startAddr.latitude = [NSNumber numberWithDouble:newCoordinate.latitude];
self.startAddr.longitude = [NSNumber numberWithDouble:newCoordinate.longitude];
coordinate = newCoordinate;
}
最佳答案
首先你必须像这样创建一个自定义注释:
MyAnnotation.h
#import <MapKit/MapKit.h>
@interface MyAnnotation : NSObject <MKAnnotation>{
CLLocationCoordinate2D coordinate;
}
- (id)initWithCoordinate:(CLLocationCoordinate2D)coord;
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate;
@end
MyAnnotation.m
#import "MyAnnotation.h"
@implementation MyAnnotation
@synthesize coordinate;
- (NSString *)subtitle{
return nil;
}
- (NSString *)title{
return nil;
}
-(id)initWithCoordinate:(CLLocationCoordinate2D)coord {
coordinate=coord;
return self;
}
-(CLLocationCoordinate2D)coord
{
return coordinate;
}
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate {
coordinate = newCoordinate;
}
@end
之后,你只需要像这样使用你的注解:
// Assuming you have imported MyAnnotation.h and you have a self.map property pointing to a MKMapView
MyAnnotation *myPin = [[MyAnnotation alloc] initWithCoordinate:self.map.centerCoordinate]; // Or whatever coordinates...
[self.map addAnnotation:myPin];
此外,您必须为注释返回一个 View 。您可以这样做:
- (MKAnnotationView *) mapView: (MKMapView *) mapView viewForAnnotation: (id<MKAnnotation>) annotation {
MKPinAnnotationView *pin = (MKPinAnnotationView *) [self.map dequeueReusableAnnotationViewWithIdentifier: @"myPin"];
if (pin == nil) {
pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"myPin"] autorelease]; // If you use ARC, take out 'autorelease'
} else {
pin.annotation = annotation;
}
pin.animatesDrop = YES;
pin.draggable = YES;
return pin;
}
然后,一旦您在 Controller 中采用了 MKMapViewDelegate 协议(protocol),您就可以像这样拖动后获取图钉的坐标:
- (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)annotationView
didChangeDragState:(MKAnnotationViewDragState)newState
fromOldState:(MKAnnotationViewDragState)oldState
{
if (newState == MKAnnotationViewDragStateEnding)
{
CLLocationCoordinate2D droppedAt = annotationView.annotation.coordinate;
NSLog(@"Pin dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);
}
}
关于iphone - 在 map View 上拖动注释图钉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10733564/
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭5年前。Improvethisquestion我审查了一些用Ruby编写的专业代码,没有发现任何评论。代码读起来相当清晰,但没有self记录。我应该期望专业编写的Ruby代码有注释吗?或者,是否有一些Ruby原则认为注释不是必需的?
我有一些如下所示的Ruby代码:#some_string="{really?}"大括号需要是字符串的一部分。这一行是注释掉的代码,我想保留在那里。我还使用YARD来记录代码,所以当我运行yarddoc时,它(自然地)会发出无法“真正”链接的警告。有没有办法让YARD忽略注释掉的代码? 最佳答案 IsthereawayIcantellYARDtoignorecommentedoutcode?一方面,YARD记录支持Rdoc标记。并且Rdoc被记录为支持几种隐藏部件的方法。RDocstopsprocessingcommentsifitf
查看用Ruby编写的源代码,比如Rails,我经常看到小代码用tt标签包裹,比如rails/activesupport/core_ext/array/access.rb#Equaltoself[2].##%w(abcde).third#=>"c"defthirdself[2]end这背后的约定是什么,何时以及为何决定使用此表示法? 最佳答案 是的,我错了,抱歉这是特殊RDoc系统的一部分。Non-verbatimtextcanbemarkedup:italic:wordortextbold:wordortexttypewriter:
在SLIM中编写HTML注释时:/!Thefirstlineofcomments/!Thesecondlineofcomments输出变成所有其他生成的HTML格式和缩进都正确,因为我将pretty设置为true我正在编写供其他人使用的模板,因此我需要带有换行符的注释以提高可读性。 最佳答案 你可以像这样在Slim中实现单行多行注释:/!ThefirstlineofcommentsThesecondlineofcomments应该输出这个: 关于ruby-在Slim模板中,不同行的HTM
我正在构建一个Ruby脚本来更改config/locales/*.ymlRails语言环境文件的内容。这些文件包含许多有用的注释和变量。通过加载、更新和转储它们,我丢失了这些注释和变量。如何在保留注释和变量的同时以编程方式更新YAML文件? 最佳答案 我不认为你可以。YAML会忽略数据文件中的注释,但不会解析它们,因此它们会在文件加载时被丢弃。加载文件后,它们就消失了。我能想到的做你想做的唯一方法是在YAML之外打开文件,然后编写注释,然后写入使用to_yaml创建的YAML内容。像这样的东西:require'yaml'data={
我正在构建一个与RubyonRails后端对话的iPhone应用程序。RubyonRails应用程序还将为Web用户提供服务。restful_authentication插件是提供快速和可定制的用户身份验证的绝佳方式。但是,我希望iPhone应用程序的用户在新列中存储一个由手机的唯一标识符([[UIDevicedevice]uniqueIdentifier])自动创建的帐户。稍后,当用户准备好创建用户名/密码时,帐户将更新为包含用户名和密码,iPhone唯一标识符保持不变。用户在设置用户名/密码之前不能访问该网站。然而,他们可以使用iPhone应用程序,因为该应用程序可以使用它的标识符
我有一个使用deviseonrails3的应用程序。我想启用http身份验证,以便我可以从iPhone应用程序向我的网络应用程序进行身份验证。如何从我的iPhone应用程序进行身份验证以进行设计?这安全吗?还是我应该进行不同的身份验证? 最佳答案 从设计的角度来看,您有3个选择:1)使用基本的http身份验证:您的iPhone应用程序有一个secretkey-这是在您的iPhone应用程序代码中烘焙的-用于对网络应用程序的每个请求进行身份验证。Google搜索:“设计基本的http身份验证”2)您可以通过在您的iPhone应用程序中
我在Ruby包中没有看到任何可以帮助我向大块代码添加注释的东西。我在网上找到的此类快捷方式的链接似乎不再有效。 最佳答案 它不是特定于Ruby的:相同的快捷方式适用于所有语言,并且可以在“Source”包中找到。是Cmd/ 关于ruby-有没有办法在TextMate中注释掉一大块代码?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/9486020/
这个问题在这里已经有了答案:关闭13年前。PossibleDuplicates:HowcanIdevelopforiPhoneusingaWindowsdevelopmentmachine?我想为我妻子的手机构建一个iPhone应用程序,但我对购买Mac作为一次性工作的开发平台不感兴趣。应用程序:应该在iPhone上独立运行(即没有网络连接)完全可以接受使用iPhoneJavascript库之一创建的GUI会做一些数据库IO来读取和更新数据没有商业值(value),永远不会被任何人使用这是我的想法:越狱iPhone在iPhone上安装Ruby+Sinatra使用Sinatra编写应用程
我正在试验iPhoneSDK并在Nic博士的rbiPhoneTest项目中做一些TDD。我想知道有多少人(如果有的话)成功地使用了这个或任何其他iPhone/Cocoa测试框架?更重要的是,我想知道如何最好地断言专有的二进制请求/响应协议(protocol)。这个想法是通过网络发送二进制请求并接收二进制响应。请求和响应是使用byteand'ing和or'ing创建的。我正在使用黄金副本模式来测试我的请求。这是我到目前为止所拥有的。不要笑,因为我是ObjectiveC和Ruby的新手:requireFile.dirname(__FILE__)+'/test_helper'require'