マップに線を表示する方法を熱心に考えています。タッチ開始からタッチ終了まで線を引こうとしています。
現時点では、私が触れた場所の座標を取り、それを配列に追加し、最後に触れた場所のコードを取り、それを配列に追加してから線を引きます。これは以下の私のコードです。
悲しいことにコードを実行したとき、私の行は表示されませんでした。しかし、何が欠けているのか疑問に思って座っている間 (座標が配列に保存されているため)、イライラして画面をスワイプし続けたところ、突然線が表示されました! 私はプログラムを閉じて同じことをやり直しました。私が知る限り、線はランダムに、間違った座標で描かれています。
私が変更され
CLLocationCoordinate2D obh1=CLLocationCoordinate2DMake
(startTouch.latitude, startTouch.longitude);
に
CLLocationCoordinate2D obh1=CLLocationCoordinate2DMake
(startTouch.longitude, startTouch.latitude);
どういうわけか自分の座標を混ぜていたが、同じことが起こっているかどうかを確認する. 線はランダムな位置に描かれています。
誰かがこれを解決するために少し助けてくれれば幸いです。
私はいくつかの作業を行いましたが、これが私が思いついたものです。マップをクリックすると、4 本のライン ポイントが表示されます。それらはタッチの終わり、タッチの始まりです。それらがどこから来ているのかわかりませんが、私のコードからではありません。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Touches began");
UITouch *touch = [touches anyObject];
// Get the specific point that was touched
CGPoint point = [touch locationInView:self.view];
//convert toc
CLLocationCoordinate2D startTouch
=[self.map convertPoint:point toCoordinateFromView:_map];
CLLocationCoordinate2D obh1=CLLocationCoordinate2DMake(startTouch.latitude, startTouch.longitude);
[arrayOfLine addObject: [NSValue valueWithMKCoordinate:obh1]];
NSLog(@"array: %@", arrayOfLine);
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Touches END");
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
CLLocationCoordinate2D endTouch
=[self.map convertPoint:point toCoordinateFromView:_map];
CLLocationCoordinate2D obh=CLLocationCoordinate2DMake(endTouch.latitude, endTouch.longitude);
[arrayOfLine addObject: [NSValue valueWithMKCoordinate:obh]];
NSLog(@"array: %@", arrayOfLine);
MKPolyline *polygon = [MKPolyline polylineWithCoordinates:&obh count:[arrayOfLine count]];
[_map addOverlay:polygon];
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKPolyline class]])
{
MKPolylineView *overlayView = [[MKPolylineView alloc] initWithPolyline:overlay];
overlayView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
overlayView.lineWidth = 3;
return overlayView;
}
return nil;
}