我正在使用apple的mapkit框架制作应用程序。我想要做的是从你按的位置获取经度和纬度。我使用以下代码从用户当前位置获取坐标:
- (IBAction)longpressToGetLocation:(id)sender {
CLLocationCoordinate2D location = [[[self.mapView userLocation] location] coordinate];
NSLog(@"Location found from Map: %f %f",location.latitude,location.longitude);
}
如何让代码显示按下的位置而不是userlocation?
答案 0 :(得分:56)
首先,使用UIGestureRecognizer
代替IBAction
- (void)longpressToGetLocation:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateBegan)
return;
CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];
CLLocationCoordinate2D location =
[self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];
NSLog(@"Location found from Map: %f %f",location.latitude,location.longitude);
}
Swift 2:
@IBAction func revealRegionDetailsWithLongPressOnMap(sender: UILongPressGestureRecognizer) {
if sender.state != UIGestureRecognizerState.Began { return }
let touchLocation = sender.locationInView(protectedMapView)
let locationCoordinate = protectedMapView.convertPoint(touchLocation, toCoordinateFromView: protectedMapView)
print("Tapped at lat: \(locationCoordinate.latitude) long: \(locationCoordinate.longitude)")
}
斯威夫特3:
@IBAction func revealRegionDetailsWithLongPressOnMap(sender: UILongPressGestureRecognizer) {
if sender.state != UIGestureRecognizerState.began { return }
let touchLocation = sender.location(in: protectedMapView)
let locationCoordinate = protectedMapView.convert(touchLocation, toCoordinateFrom: protectedMapView)
print("Tapped at lat: \(locationCoordinate.latitude) long: \(locationCoordinate.longitude)")
}
答案 1 :(得分:6)
10-09 11:16:47.471 5464-5464/com.example.michaelheneghan.example1 I/art﹕ Not late-enabling -Xcheck:jni (already on)
10-09 11:16:47.471 5464-5464/com.example.michaelheneghan.example1 I/art﹕ Late-enabling JIT
10-09 11:16:47.473 5464-5464/com.example.michaelheneghan.example1 I/art﹕ JIT created with code_cache_capacity=2MB compile_threshold=1000
10-09 11:16:47.497 5464-5464/com.example.michaelheneghan.example1 W/System﹕ ClassLoader referenced unknown path: /data/app/com.example.michaelheneghan.example1-2/lib/x86
10-09 11:16:47.599 5464-5464/com.example.michaelheneghan.example1 D/AndroidRuntime﹕ Shutting down VM
10-09 11:16:47.599 5464-5464/com.example.michaelheneghan.example1 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.michaelheneghan.example1, PID: 5464
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.michaelheneghan.example1/com.example.michaelheneghan.example1.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:4309)
at android.view.ViewGroup.addView(ViewGroup.java:4145)
at android.view.ViewGroup.addView(ViewGroup.java:4117)
at com.example.michaelheneghan.example1.MainActivity.onCreate(MainActivity.java:105)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Swift 4。
class mapviewCtrl: UIViewController, UIGestureRecognizerDelegate
{
@IBOutlet var mapViewVar: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
let lpgr = UILongPressGestureRecognizer(target: self, action:"handleLongPress:")
lpgr.minimumPressDuration = 0.5
lpgr.delaysTouchesBegan = true
lpgr.delegate = self
self.mapViewVar.addGestureRecognizer(lpgr)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) {
if gestureReconizer.state != UIGestureRecognizerState.Ended {
let touchLocation = gestureReconizer.locationInView(mapViewVar)
let locationCoordinate = mapViewVar.convertPoint(touchLocation,toCoordinateFromView: mapViewVar)
println("Tapped at lat: \(locationCoordinate.latitude) long: \(locationCoordinate.longitude)")
return
}
if gestureReconizer.state != UIGestureRecognizerState.Began {
return
}
}
答案 2 :(得分:3)
swift 3 的另一种方式是这个:这是一个包含少量代码片段的混搭 - 使用覆盖,您只需在一个地方更新代码,这样可以方便,增强模块性,并删除假设和潜在的崩溃点。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// Let's put in a log statement to see the order of events
print(#function)
for touch in touches {
let touchPoint = touch.location(in: self.mapView)
let location = self.mapView.convert(touchPoint, toCoordinateFrom: self.mapView)
print ("\(location.latitude), \(location.longitude)")
}
}