Maps SDK สำหรับ iOS นำเสนอหลากหลายวิธีเพื่อให้คุณเพิ่มรูปร่างลงในแผนที่ ระบบรองรับรูปร่างต่อไปนี้
- เส้นประกอบคือชุดของเส้นส่วนต่างๆ ที่เชื่อมต่อกันซึ่งสามารถก่อตัวเป็นรูปร่างใดก็ได้ที่คุณต้องการ และสามารถใช้เพื่อทำเครื่องหมายเส้นทางและเส้นทางบนแผนที่ได้
- รูปหลายเหลี่ยมคือรูปร่างปิดซึ่งสามารถใช้เพื่อทำเครื่องหมายพื้นที่ต่างๆ บนแผนที่
- วงกลมคือการฉายภาพวงกลมบนพื้นผิวโลกที่แม่นยำทางภูมิศาสตร์
คุณสามารถแก้ไขลักษณะที่ปรากฏของแต่ละรูปร่างได้หลายวิธี
เส้นประกอบ
เส้นประกอบช่วยให้คุณวาดเส้นบนแผนที่ได้ ออบเจ็กต์ GMSPolyline
แสดงลำดับตำแหน่งตามลำดับ ซึ่งแสดงเป็นชุดส่วนต่างๆ ของเส้น คุณตั้งค่าสีของเส้นประกอบได้ด้วย GMSStrokeStyle
หากต้องการสร้างเส้นประกอบ คุณจะต้องระบุเส้นทางของเส้นดังกล่าวโดยสร้างออบเจ็กต์ GMSMutablePath
ที่สอดคล้องกันซึ่งมีจุด 2 จุดขึ้นไป
CLLocationCoordinate2D
แต่ละจุดแทนจุดบนพื้นผิวโลก จะมีการวาดส่วนของเส้นระหว่างจุดต่างๆ ตามลำดับที่คุณเพิ่มลงในเส้นทาง คุณเพิ่มจุดลงในเส้นทางได้ด้วยเมธอด addCoordinate:
หรือ addLatitude:longitude:
Swift
let path = GMSMutablePath() path.add(CLLocationCoordinate2D(latitude: -33.85, longitude: 151.20)) path.add(CLLocationCoordinate2D(latitude: -33.70, longitude: 151.40)) path.add(CLLocationCoordinate2D(latitude: -33.73, longitude: 151.41)) let polyline = GMSPolyline(path: path)
Objective-C
GMSMutablePath *path = [GMSMutablePath path]; [path addCoordinate:CLLocationCoordinate2DMake(-33.85, 151.20)]; [path addCoordinate:CLLocationCoordinate2DMake(-33.70, 151.40)]; [path addCoordinate:CLLocationCoordinate2DMake(-33.73, 151.41)]; GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
การเพิ่มเส้นประกอบ
- สร้างออบเจ็กต์
GMSMutablePath
- กำหนดจุดในเส้นทางด้วยเมธอด
addCoordinate:
หรือaddLatitude:longitude:
- สร้างอินสแตนซ์ออบเจ็กต์
GMSPolyline
ใหม่โดยใช้เส้นทางเป็นอาร์กิวเมนต์ - ตั้งค่าพร็อพเพอร์ตี้อื่นๆ เช่น
strokeWidth
และstrokeColor
ตามความจำเป็น - ตั้งค่าพร็อพเพอร์ตี้
map
ของGMSPolyline
- เส้นประกอบจะปรากฏในแผนที่
ข้อมูลโค้ดต่อไปนี้จะเพิ่มสี่เหลี่ยมผืนผ้าลงในแผนที่
Swift
let rectanglePath = GMSMutablePath() rectanglePath.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.0)) rectanglePath.add(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.0)) rectanglePath.add(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.2)) rectanglePath.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.2)) rectanglePath.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.0)) let rectangle = GMSPolyline(path: path) rectangle.map = mapView
Objective-C
GMSMutablePath *rectanglePath = [GMSMutablePath path]; [rectanglePath addCoordinate:CLLocationCoordinate2DMake(37.36, -122.0)]; [rectanglePath addCoordinate:CLLocationCoordinate2DMake(37.45, -122.0)]; [rectanglePath addCoordinate:CLLocationCoordinate2DMake(37.45, -122.2)]; [rectanglePath addCoordinate:CLLocationCoordinate2DMake(37.36, -122.2)]; [rectanglePath addCoordinate:CLLocationCoordinate2DMake(37.36, -122.0)]; GMSPolyline *rectangle = [GMSPolyline polylineWithPath:path]; rectangle.map = mapView;
กำลังนำเส้นประกอบออก
คุณนำเส้นประกอบออกจากแผนที่ได้โดยตั้งค่าพร็อพเพอร์ตี้ map
ของ GMSPolyline
เป็น nil
หรือจะนำการวางซ้อนทั้งหมด (รวมทั้งเส้นประกอบและรูปร่างอื่นๆ) ออกจากแผนที่ได้โดยเรียกใช้เมธอด GMSMapView
clear
Swift
mapView.clear()
Objective-C
[mapView clear];
การปรับแต่งเส้นประกอบ
ออบเจ็กต์ GMSPolyline
มีพร็อพเพอร์ตี้มากมายสำหรับการควบคุมรูปลักษณ์ของเส้น โดยรองรับตัวเลือกต่อไปนี้
strokeWidth
- ความกว้างของทั้งเส้น เป็นจุดบนหน้าจอ ค่าเริ่มต้นคือ 1 ความกว้างไม่ปรับตามขนาดเมื่อซูมแผนที่
geodesic
-
เมื่อ
YES
ให้แสดงผลขอบเส้นประกอบนี้เป็นเรขาคณิต ส่วนที่เป็นธรณีแปรสัณฐานเคลื่อนที่ไปตามเส้นทางที่สั้นที่สุดบนพื้นผิวโลก และอาจปรากฏเป็นเส้นโค้งบนแผนที่ที่มีเส้นโครงเมอร์เคเตอร์ ส่วนที่ไม่ได้มาจากภูมิศาสตร์จะถูกวาดเป็นเส้นตรงบนแผนที่ ค่าเริ่มต้นคือNO
spans
- ใช้เพื่อระบุสีของเส้นประกอบอย่างน้อย 1 ส่วน พร็อพเพอร์ตี้ Span คืออาร์เรย์ของออบเจ็กต์
GMSStyleSpan
การตั้งค่าพร็อพเพอร์ตี้spans
เป็นวิธีที่แนะนำในการเปลี่ยนสีของเส้นประกอบ strokeColor
- ออบเจ็กต์
UIColor
ที่ระบุสีของเส้นประกอบ ค่าเริ่มต้นคือblueColor
ระบบจะละเว้นพร็อพเพอร์ตี้strokeColor
หากตั้งค่าspans
ข้อมูลโค้ดต่อไปนี้เพิ่มเส้นโพลีไลน์แบบหนาจากเมลเบิร์นไปยังเพิร์ธ โดยมีการประมาณการในช่วงทางภูมิศาสตร์
Swift
let path = GMSMutablePath() path.addLatitude(-37.81319, longitude: 144.96298) path.addLatitude(-31.95285, longitude: 115.85734) let polyline = GMSPolyline(path: path) polyline.strokeWidth = 10.0 polyline.geodesic = true polyline.map = mapView
Objective-C
GMSMutablePath *path = [GMSMutablePath path]; [path addLatitude:-37.81319 longitude:144.96298]; [path addLatitude:-31.95285 longitude:115.85734]; GMSPolyline *polyline = [GMSPolyline polylineWithPath:path]; polyline.strokeWidth = 10.f; polyline.geodesic = YES; polyline.map = mapView;
หากต้องการแก้ไขเส้นประกอบหลังจากเพิ่มลงในแผนที่แล้ว ให้จับออบเจ็กต์ GMSPolyline
ไว้
Swift
polyline.strokeColor = .blue
Objective-C
polyline.strokeColor = [UIColor blueColor];
เปลี่ยนสีของเส้นประกอบ
เส้นประกอบจะปรากฏเป็นชุดของส่วนต่างๆ บนแผนที่ คุณจะเปลี่ยนสีของแต่ละส่วนหรือจะเปลี่ยนสีทั้งเส้นก็ได้โดยใช้พร็อพเพอร์ตี้ spans
แม้ว่าพร็อพเพอร์ตี้นี้จะช่วยให้คุณควบคุมการลงสีเส้นประกอบโดยละเอียดได้ แต่ก็มีความสะดวกหลายอย่างที่ให้คุณใช้รูปแบบเดียวกับทั้งเส้นได้
ข้อมูลโค้ดด้านล่างใช้เมธอด spanWithColor:
เพื่อเปลี่ยนสีของเส้นทั้งบรรทัดเป็นสีแดง
Swift
polyline.spans = [GMSStyleSpan(color: .red)]
Objective-C
polyline.spans = @[[GMSStyleSpan spanWithColor:[UIColor redColor]]];
หรือหากคุณมีสิทธิ์เข้าถึงออบเจ็กต์ GMSStrokeStyle
อยู่แล้ว คุณจะใช้เมธอด spanWithStyle:
ก็ได้
Swift
let solidRed = GMSStrokeStyle.solidColor(.red) polyline.spans = [GMSStyleSpan(style: solidRed)]
Objective-C
GMSStrokeStyle *solidRed = [GMSStrokeStyle solidColor:[UIColor redColor]]; polyline.spans = @[[GMSStyleSpan spanWithStyle:solidRed]];
ก่อนหน้า Maps SDK เวอร์ชัน 1.7 สำหรับ iOS พร็อพเพอร์ตี้ strokeColor
รายการเดียวพร้อมให้ตั้งค่าสีทั้งหมดของ GMSPolyline
พร็อพเพอร์ตี้ spans
มีความสําคัญเหนือ strokeColor
Swift
polyline.strokeColor = .red
Objective-C
polyline.strokeColor = [UIColor redColor];
รูปแบบ
หากแอปใช้สีเส้นโครงร่างเดียวกันหลายครั้ง การกำหนดรูปแบบที่นำมาใช้ใหม่ได้อาจมีประโยชน์สำหรับคุณ ระบุรูปแบบเส้นประกอบโดยใช้ออบเจ็กต์ GMSStrokeStyle
รูปแบบเส้นโครงร่างอาจเป็นสีทึบ
หรือการไล่ระดับจากสีหนึ่งเป็นอีกสีหนึ่ง เมื่อสร้างสไตล์แล้ว คุณจะใช้รูปแบบนั้นกับ GMSStyleSpan
ได้ด้วยเมธอด spanWithStyle:
Swift
// Create two styles: one that is solid blue, and one that is a gradient from red to yellow let solidBlue = GMSStrokeStyle.solidColor(.blue) let solidBlueSpan = GMSStyleSpan(style: solidBlue) let redYellow = GMSStrokeStyle.gradient(from: .red, to: .yellow) let redYellowSpan = GMSStyleSpan(style: redYellow)
Objective-C
// Create two styles: one that is solid blue, and one that is a gradient from red to yellow GMSStrokeStyle *solidBlue = [GMSStrokeStyle solidColor:[UIColor blueColor]]; GMSStyleSpan *solidBlueSpan = [GMSStyleSpan spanWithStyle:solidBlue]; GMSStrokeStyle *redYellow = [GMSStrokeStyle gradientFromColor:[UIColor redColor] toColor:[UIColor yellowColor]]; GMSStyleSpan *redYellowSpan = [GMSStyleSpan spanWithStyle:redYellow];
รูปแบบของ span
จะดำเนินต่อไปจนถึงจุดสิ้นสุดของเส้นประกอบ หรือจนกว่าจะตั้งค่ารูปแบบใหม่ คุณสามารถเปลี่ยนสีของเส้นทั้งเส้นได้โดยตั้งค่าพร็อพเพอร์ตี้ spans
ของเส้นประกอบเป็น GMSStyleSpan
เส้นเดียว ตัวอย่างนี้แสดงวิธีใช้งานการไล่ระดับสีกับความยาวทั้งหมดของเส้นประกอบ
Swift
polyline.spans = [GMSStyleSpan(style: redYellow)]
Objective-C
polyline.spans = @[[GMSStyleSpan spanWithStyle:redYellow]];
การเปลี่ยนสีของส่วนของเส้นตรงแต่ละส่วน
หากต้องการจัดรูปแบบส่วนของเส้นประกอบแต่ละส่วนแยกกัน ก็ทำได้โดยการสร้างอาร์เรย์ของออบเจ็กต์ GMSStyleSpan
แล้วส่งผ่านไปยังพร็อพเพอร์ตี้ spans
โดยค่าเริ่มต้น แต่ละรายการในอาร์เรย์จะกำหนดสีของส่วนของเส้นที่เกี่ยวข้อง หากมีองค์ประกอบในอาร์เรย์มากกว่ากลุ่มในบรรทัด องค์ประกอบส่วนเกินจะถูกละเว้น หากมีจำนวนองค์ประกอบในอาร์เรย์น้อยลง GMSStyleSpan
สุดท้ายจะอธิบายสีของส่วนที่เหลือของบรรทัด
คุณใช้บล็อกสีและ/หรือเส้นประกอบการไล่ระดับสีเพื่อระบุการเปลี่ยนแปลงของเส้นประกอบ เช่น ระดับความสูงหรือความเร็วได้ ตัวอย่างข้อมูลด้านล่างจะกำหนดสีของเส้นประกอบ 2 ส่วนแรกเป็นสีแดง และส่วนที่เหลือของเส้นเป็นการไล่ระดับจากสีแดงเป็นสีเหลือง
Swift
polyline.spans = [ GMSStyleSpan(style: solidRed), GMSStyleSpan(style: solidRed), GMSStyleSpan(style: redYellow) ]
Objective-C
polyline.spans = @[[GMSStyleSpan spanWithStyle:solidRed], [GMSStyleSpan spanWithStyle:solidRed], [GMSStyleSpan spanWithStyle:redYellow]];
คุณใช้เมธอด spanWithStyle:segments:
เพื่อตั้งค่าสไตล์สำหรับหลายกลุ่มพร้อมกันได้ ตัวอย่างเช่น โค้ดต่อไปนี้เทียบเท่ากับโค้ดข้างต้น
ระบบจะไม่สนใจความยาวของส่วนของ GMSStyleSpan
สุดท้ายเสมอ เนื่องจากใช้รูปแบบดังกล่าวเพื่ออธิบายส่วนที่เหลือของบรรทัด
Swift
polyline.spans = [ GMSStyleSpan(style: solidRed, segments:2), GMSStyleSpan(style: redYellow, segments:10) ]
Objective-C
polyline.spans = @[[GMSStyleSpan spanWithStyle:solidRed segments:2], [GMSStyleSpan spanWithStyle:redYellow segments:10]];
กลุ่มเศษส่วน
คุณอาจระบุกลุ่มเป็นค่าเศษส่วนได้เช่นกัน ซึ่งจะใช้รูปแบบนี้กับจำนวนเศษส่วนของกลุ่ม ซึ่งอาจส่งผลให้เกิดการแยกในกลุ่มเดียว GMSStyleSpan
แต่ละรายการจะเริ่มจากต่อจากส่วนที่ 1 โดยในตัวอย่างด้านล่าง สีเทาจะเริ่มจาก 1⁄2 ถึงส่วนที่ 2 และต่อเนื่องไปจนถึง 1⁄2 จนถึงส่วนที่ 3
Swift
polyline.spans = [ GMSStyleSpan(style: solidRed, segments: 2.5), GMSStyleSpan(color: .gray), GMSStyleSpan(color: .purple, segments: 0.75), GMSStyleSpan(style: redYellow) ]
Objective-C
polyline.spans = @[[GMSStyleSpan spanWithStyle:solidRed segments:2.5], [GMSStyleSpan spanWithColor:[UIColor grayColor]], [GMSStyleSpan spanWithColor:[UIColor purpleColor] segments:0.75], [GMSStyleSpan spanWithStyle:redYellow]];
การเพิ่มรูปแบบสีที่ซ้ำกันในเส้นประกอบ
หากต้องการเพิ่มรูปแบบลงในเส้นประกอบ ให้ใช้ยูทิลิตี GMSStyleSpans
ใน GMSGeometryUtils
เมธอด GMSStyleSpans
จะยอมรับอาร์เรย์ 2 รายการที่กำหนดรูปแบบที่ซ้ำกัน อาร์เรย์หนึ่งจะกำหนดรูปแบบที่ควรทำซ้ำ ส่วนอีกอาร์เรย์กำหนดช่วงเวลาของการทำซ้ำ เมื่อใช้ร่วมกัน คุณจะสร้างรูปแบบที่นำไปใช้กับเส้นประกอบใดก็ได้ ไม่ว่าจะมีความยาวหรือจำนวนกลุ่มเท่าใดก็ตาม
ตัวอย่างเช่น ข้อมูลโค้ดด้านล่างระบุเส้นประกอบที่มีรูปแบบสลับสีขาวดำ ความยาวของบรรทัดจะถือว่าเป็นเมตรตามเส้น Rhumb (ใน Mercator ถือว่าเป็นเส้นตรง) เนื่องจากระบุประเภทเป็น kGMSLengthRhumb
Swift
let styles = [ GMSStrokeStyle.solidColor(.white), GMSStrokeStyle.solidColor(.black) ] let lengths: [NSNumber] = [100000, 50000] polyline.spans = GMSStyleSpans( polyline.path!, styles, lengths, GMSLengthKind.rhumb )
Objective-C
NSArray *styles = @[[GMSStrokeStyle solidColor:[UIColor whiteColor]], [GMSStrokeStyle solidColor:[UIColor blackColor]]]; NSArray *lengths = @[@100000, @50000]; polyline.spans = GMSStyleSpans(polyline.path, styles, lengths, kGMSLengthRhumb);
โพลีไลน์แบบสไปรท์สแตมป์
เส้นโพลีเส้นตรงแบบ Sprite Stamped ช่วยให้คุณสร้างเส้นประกอบโดยใช้รูปภาพบิตแมปซ้ำกันได้ รูปร่างต่างๆ แสดงโดยมีเส้นขีดบนพื้นหลังที่ชัดเจน แต่ตราประทับไม่ได้ถูกตัดรอบมุมเส้น ทำให้มีประโยชน์ในสถานการณ์ต่างๆ เช่น จุดสำหรับแสดงเส้นทางเดินเท้า
คุณสามารถใช้ฟีเจอร์นี้โดยใช้ GMSSpriteStyle
และตั้งค่าเป็นการประทับเวลาโดยใช้พร็อพเพอร์ตี้ stampStyle
ของ GMSStrokeStyle
Swift
let path = GMSMutablePath() path.addLatitude(-37.81319, longitude: 144.96298) path.addLatitude(-31.95285, longitude: 115.85734) let polyline = GMSPolyline(path: path) polyline.strokeWidth = 20 let image = UIImage(named: "imageFromBundleOrAsset")! // Image could be from anywhere let stampStyle = GMSSpriteStyle(image: image) let transparentStampStroke = GMSStrokeStyle.transparentStroke(withStamp: stampStyle) let span = GMSStyleSpan(style: transparentStampStroke) polyline.spans = [span] polyline.map = mapView
Objective-C
GMSMutablePath *path = [GMSMutablePath path]; [path addLatitude:-37.81319 longitude:144.96298]; [path addLatitude:-31.95285 longitude:115.85734]; polyline.strokeWidth = 20; GMSPolyline *polyline = [GMSPolyline polylineWithPath:path]; UIImage *image = [UIImage imageNamed:@"imageFromBundleOrAsset"]; GMSStrokeStyle *transparentStampStroke = [GMSStrokeStyle transparentStrokeWithStampStyle:[GMSSpriteStyle spriteStyleWithImage:image]]; GMSStyleSpan *span = [GMSStyleSpan spanWithStyle:transparentStampStroke]; polyline.spans = @[span]; polyline.map = _mapView;
โพลีไลน์ประทับพื้นผิว
เส้นประกอบที่มีการประทับพื้นผิวช่วยให้คุณสร้างเส้นประกอบโดยใช้พื้นผิวซ้ำๆ ตามที่คุณต้องการได้ รูปทรงอาจแสดงด้วยสีทึบที่ชัดเจน หรือการไล่ระดับสีพื้นหลัง พื้นผิวจะปรับขนาดเมื่อระดับการซูมเปลี่ยนแปลง รูปภาพที่สิ้นสุดหรือจุดเริ่มต้นของเส้นทางหรือจุดของเส้นทางจะถูกตัดออกในระดับการซูมบางระดับ
คุณสามารถใช้ฟีเจอร์นี้โดยใช้ GMSTextureStyle
และตั้งค่าเป็นการประทับเวลาโดยใช้พร็อพเพอร์ตี้ stampStyle
ของ GMSStrokeStyle
Swift
let path = GMSMutablePath() path.addLatitude(-37.81319, longitude: 144.96298) path.addLatitude(-31.95285, longitude: 115.85734) let polyline = GMSPolyline(path: path) polyline.strokeWidth = 20 let redWithStamp = GMSStrokeStyle.solidColor(.red) let image = UIImage(named: "imageFromBundleOrAsset")! // Image could be from anywhere redWithStamp.stampStyle = GMSTextureStyle(image: image) let span = GMSStyleSpan(style: redWithStamp) polyline.spans = [span] polyline.map = mapView
Objective-C
GMSMutablePath *path = [GMSMutablePath path]; [path addLatitude:-37.81319 longitude:144.96298]; [path addLatitude:-31.95285 longitude:115.85734]; GMSPolyline *polyline = [GMSPolyline polylineWithPath:path]; polyline.strokeWidth = 20; GMSStrokeStyle *redWithStamp = [GMSStrokeStyle solidColor:[UIColor redColor]]; UIImage *image = [UIImage imageNamed:@"imageFromBundleOrAsset"]; // Image could be from anywhere redWithStamp.stampStyle = [GMSTextureStyle textureStyleWithImage:image]; GMSStyleSpan *span = [GMSStyleSpan spanWithStyle:redWithStamp]; polyline.spans = @[span]; polyline.map = _mapView;
ความสามารถของแผนที่
พร็อพเพอร์ตี้ mapCapabilities
บน GMSMapView
เพิ่มการตรวจสอบแบบเป็นโปรแกรมสำหรับฟีเจอร์เฉพาะแผนที่ ซึ่งจะเป็นประโยชน์เมื่อต้องการทราบว่าแผนที่บางรายการ capabilities
พร้อมใช้งานหรือไม่ก่อนที่จะเรียกใช้ API ที่เฉพาะเจาะจง คำค้นหานี้จะระบุว่ามุมมองแผนที่รองรับโพลีไลน์สไปรท์ที่ประทับตราหรือไม่
Swift
let path = GMSMutablePath() path.addLatitude(-37.81319, longitude: 144.96298) path.addLatitude(-31.95285, longitude: 115.85734) let polyline = GMSPolyline(path: path) polyline.strokeWidth = 20 let image = UIImage(named: "imageFromBundleOrAsset")! // Image could be from anywhere let spans: [GMSStyleSpan] if (mapView.mapCapabilities.contains(.spritePolylines)) { let spriteStyle = GMSSpriteStyle(image: image) let stroke = GMSStrokeStyle.transparentStroke(withStamp: spriteStyle) spans = [ GMSStyleSpan(style: stroke) ] } else { let stroke = GMSStrokeStyle.solidColor(.clear) stroke.stampStyle = GMSTextureStyle(image: image) spans = [ GMSStyleSpan(style: stroke) ] } polyline.spans = spans polyline.map = mapView
Objective-C
GMSMutablePath *path = [GMSMutablePath path]; [path addLatitude:-37.81319 longitude:144.96298]; [path addLatitude:-31.95285 longitude:115.85734]; UIImage *_Nonnull image = [UIImage imageNamed:@"imageFromBundleOrAsset"]; // Image could be from anywhere NSArray<GMSStyleSpan *> * spans; if (_mapView.mapCapabilities & GMSMapCapabilityFlagsSpritePolylines) { GMSSpriteStyle *spriteStyle = [GMSSpriteStyle spriteStyleWithImage:image]; GMSStrokeStyle *stroke = [GMSStrokeStyle transparentStrokeWithStampStyle:spriteStyle]; spans = @[ [GMSStyleSpan spanWithStyle:stroke] ]; } else { GMSStrokeStyle *stroke = [GMSStrokeStyle solidColor:UIColor.clearColor]; stroke.stampStyle = [GMSTextureStyle textureStyleWithImage:image]; spans = @[ [GMSStyleSpan spanWithStyle:stroke] ]; } GMSPolyline *polyline = [GMSPolyline polylineWithPath:path]; polyline.strokeWidth = 20; polyline.spans = spans; polyline.map = _mapView;
รูปแบบนี้ช่วยให้คุณสมัครรับข้อมูลการเปลี่ยนแปลงและตอบสนองต่อการอัปเดตด้วยสถานะมุมมองแผนที่ของคุณ คุณยังใช้ didChangeMapCapabilities
ใน GMSMapViewDelegate
เพื่อรับข้อมูลอัปเดตความพร้อมให้บริการของฟีเจอร์ได้ด้วย
รูปหลายเหลี่ยม
รูปหลายเหลี่ยมคล้ายกับเส้นประกอบตรงที่ประกอบด้วยชุดของพิกัดตามลำดับ อย่างไรก็ตาม รูปหลายเหลี่ยมได้รับการออกแบบมาเพื่อระบุพื้นที่ของแข็งภายในวงปิด แทนทิศทางแบบเปิด รูปหลายเหลี่ยมกำหนดใน Maps SDK สำหรับ iOS ตามคลาส GMSPolygon
คุณเพิ่ม GMSPolygon
ลงในแผนที่ได้ด้วยวิธีเดียวกับที่ใช้เพิ่ม GMSPolyline
ก่อนอื่นให้ระบุเส้นทางโดยสร้างออบเจ็กต์ GMSMutablePath
ที่เกี่ยวข้องและเพิ่มจุดลงในออบเจ็กต์
จุดเหล่านี้เกิดจากโครงร่างของรูปหลายเหลี่ยม CLLocationCoordinate2D
แต่ละรายการจะแทนจุดบนพื้นผิวโลก ส่วนของเส้นตรงจะวาดระหว่างจุด
ตามลำดับที่คุณเพิ่มพวกเขาลงในเส้นทาง
เพิ่มรูปหลายเหลี่ยม
- สร้างออบเจ็กต์
GMSMutablePath
- กำหนดจุดในเส้นทางด้วยเมธอด
addCoordinate:
หรือaddLatitude:longitude:
จุดเหล่านี้จะเป็นรูปเส้นขอบ ของรูปหลายเหลี่ยม - สร้างอินสแตนซ์ออบเจ็กต์
GMSPolygon
ใหม่โดยใช้เส้นทางเป็นอาร์กิวเมนต์ - ตั้งค่าพร็อพเพอร์ตี้อื่นๆ เช่น
strokeWidth
,strokeColor
และfillColor
ตามต้องการ - กำหนดรูปหลายเหลี่ยมให้กับออบเจ็กต์
GMSMapView
โดยการตั้งค่าพร็อพเพอร์ตี้GMSPolygon.map
- รูปหลายเหลี่ยมจะปรากฏบนแผนที่
ข้อมูลโค้ดต่อไปนี้จะเพิ่มสี่เหลี่ยมผืนผ้าลงในแผนที่
Swift
// Create a rectangular path let rect = GMSMutablePath() rect.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.0)) rect.add(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.0)) rect.add(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.2)) rect.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.2)) // Create the polygon, and assign it to the map. let polygon = GMSPolygon(path: rect) polygon.fillColor = UIColor(red: 0.25, green: 0, blue: 0, alpha: 0.05); polygon.strokeColor = .black polygon.strokeWidth = 2 polygon.map = mapView
Objective-C
// Create a rectangular path GMSMutablePath *rect = [GMSMutablePath path]; [rect addCoordinate:CLLocationCoordinate2DMake(37.36, -122.0)]; [rect addCoordinate:CLLocationCoordinate2DMake(37.45, -122.0)]; [rect addCoordinate:CLLocationCoordinate2DMake(37.45, -122.2)]; [rect addCoordinate:CLLocationCoordinate2DMake(37.36, -122.2)]; // Create the polygon, and assign it to the map. GMSPolygon *polygon = [GMSPolygon polygonWithPath:rect]; polygon.fillColor = [UIColor colorWithRed:0.25 green:0 blue:0 alpha:0.05]; polygon.strokeColor = [UIColor blackColor]; polygon.strokeWidth = 2; polygon.map = mapView;
คุณสามารถปรับแต่งลักษณะของรูปหลายเหลี่ยมได้ทั้งก่อนเพิ่มรูปนั้นลงในแผนที่และหลังจากที่เพิ่มรูปภาพนั้นลงในแผนที่แล้ว
การลบรูปหลายเหลี่ยม
นำรูปหลายเหลี่ยมออกโดยตั้งค่าพร็อพเพอร์ตี้ GMSPolygon.map
เป็น nil
และปลด layer
ออกจากระดับบนสุด
Swift
polygon.map = nil polygon.layer.removeFromSuperLayer()
Objective-C
polygon.map = nil; [polygon.layer removeFromSuperlayer];
วงกลม
นอกเหนือจากคลาส GMSPolygon
ทั่วไปแล้ว Maps SDK สำหรับ iOS ยังมี GMSCircle
ซึ่งช่วยให้คุณวาดวงกลมบนพื้นผิวโลกได้
ในการสร้างวงกลม คุณต้องระบุคุณสมบัติ 2 อย่างต่อไปนี้
position
ในฐานะCLLocationCoordinate2D
radius
เมตร
จากนั้นวงกลมจะหมายถึงชุดของจุดทั้งหมดบนพื้นผิวโลก
ซึ่งอยู่ห่างจาก center
ที่กำหนด radius
เมตร เนื่องจากเส้นโครงแผนที่เมอร์คาเตอร์ที่ Maps API ใช้แสดงผลเป็นทรงกลมบนพื้นราบ เส้นนี้จึงปรากฏเป็นวงกลมที่เกือบสมบูรณ์บนแผนที่เมื่ออยู่ใกล้กับเส้นศูนย์สูตร และดูเหมือนไม่เป็นวงกลมมากขึ้นเรื่อยๆ (บนหน้าจอ) ขณะที่วงกลมเคลื่อนที่ออกจากเส้นศูนย์สูตร
การเพิ่มแวดวง
ข้อมูลโค้ดต่อไปนี้จะเพิ่มวงกลมลงในแผนที่
Swift
let circleCenter = CLLocationCoordinate2D(latitude: 37.35, longitude: -122.0) let circle = GMSCircle(position: circleCenter, radius: 1000) circle.map = mapView
Objective-C
CLLocationCoordinate2D circleCenter = CLLocationCoordinate2DMake(37.35, -122.0); GMSCircle *circle = [GMSCircle circleWithPosition:circleCenter radius:1000]; circle.map = mapView;
คุณสามารถปรับแต่งลักษณะของวงกลมได้ทั้งก่อนเพิ่มลงไปในแผนที่และหลังจากที่เพิ่มลงในแผนที่แล้ว
การกำหนดค่าแวดวง
คุณระบุสีและความกว้างของเส้นโครงร่างเองได้โดยแก้ไขคุณสมบัติของ GMSCircle
โดยรองรับตัวเลือกต่อไปนี้
fillColor
- ออบเจ็กต์
UIColor
ที่ระบุสีภายในของวงกลม ค่าเริ่มต้นคือโปร่งใส strokeColor
- ออบเจ็กต์
UIColor
ที่ระบุสีโครงร่างของวงกลม ค่าเริ่มต้นคือblackColor
strokeWidth
- ความหนาของโครงร่างของวงกลมในหน่วยจุดหน้าจอ ค่าเริ่มต้นคือ 1 ความหนาไม่สามารถปรับขนาดเมื่อซูมแผนที่
ข้อมูลโค้ดต่อไปนี้จะเพิ่มวงกลมหนาสีแดงโดยมีสีแดงเข้มด้านในบางส่วน
Swift
circle.fillColor = UIColor(red: 0.35, green: 0, blue: 0, alpha: 0.05) circle.strokeColor = .red circle.strokeWidth = 5
Objective-C
circle.fillColor = [UIColor colorWithRed:0.25 green:0 blue:0 alpha:0.05]; circle.strokeColor = [UIColor redColor]; circle.strokeWidth = 5;
การสร้างรูปหลายเหลี่ยมแบบกลวง
คุณรวมเส้นทางหลายเส้นทางในออบเจ็กต์ GMSPolygon
รายการเดียวเพื่อสร้างรูปร่างที่ซับซ้อนได้ เช่น วงแหวนที่มีสีเติม หรือโดนัท (พื้นที่ที่เป็นรูปหลายเหลี่ยมปรากฏในรูปหลายเหลี่ยมเป็นรูปร่างแยกกัน) รูปทรงที่ซับซ้อนเป็นองค์ประกอบของ
เส้นทางหลายเส้น
สร้างรูปหลายเหลี่ยมพร้อมเส้นทางที่ระบุพื้นที่ที่ใหญ่ที่สุดซึ่งปกคลุมด้วยรูปหลายเหลี่ยม จากนั้นระบุพร็อพเพอร์ตี้ holes
ของรูปหลายเหลี่ยมเป็นอาร์เรย์ของวัตถุ GMSPath
อย่างน้อย 1 รายการ ซึ่งจะกำหนดรูภายในรูปหลายเหลี่ยม
หากเส้นทางขนาดเล็กถูกล้อมรอบด้วยเส้นทางขนาดใหญ่ไว้ทั้งหมด เส้นทางนั้นจะปรากฏเหมือนกับว่าชิ้นส่วนของรูปหลายเหลี่ยมได้ถูกลบออกไป
ตัวอย่างโค้ดต่อไปนี้สร้างรูปหลายเหลี่ยมที่มีสองหลุม
Swift
let hydeParkLocation = CLLocationCoordinate2D(latitude: -33.87344, longitude: 151.21135) let camera = GMSCameraPosition.camera(withTarget: hydeParkLocation, zoom: 16) let mapView = GMSMapView.map(withFrame: .zero, camera: camera) mapView.animate(to: camera) let hydePark = "tpwmEkd|y[QVe@Pk@BsHe@mGc@iNaAKMaBIYIq@qAMo@Eo@@[Fe@DoALu@HUb@c@XUZS^ELGxOhAd@@ZB`@J^BhFRlBN\\BZ@`AFrATAJAR?rAE\\C~BIpD" let archibaldFountain = "tlvmEqq|y[NNCXSJQOB[TI" let reflectionPool = "bewmEwk|y[Dm@zAPEj@{AO" let hollowPolygon = GMSPolygon() hollowPolygon.path = GMSPath(fromEncodedPath: hydePark) hollowPolygon.holes = [GMSPath(fromEncodedPath: archibaldFountain)!, GMSPath(fromEncodedPath: reflectionPool)!] hollowPolygon.fillColor = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 0.2) hollowPolygon.strokeColor = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0) hollowPolygon.strokeWidth = 2 hollowPolygon.map = mapView
Objective-C
CLLocationCoordinate2D hydeParkLocation = CLLocationCoordinate2DMake(-33.87344, 151.21135); GMSCameraPosition *camera = [GMSCameraPosition cameraWithTarget:hydeParkLocation zoom:16]; mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera]; NSString *hydePark = @"tpwmEkd|y[QVe@Pk@BsHe@mGc@iNaAKMaBIYIq@qAMo@Eo@@[Fe@DoALu@HUb@c@XUZS^ELGxOhAd@@ZB`@J^BhFRlBN\\BZ@`AFrATAJAR?rAE\\C~BIpD"; NSString *archibaldFountain = @"tlvmEqq|y[NNCXSJQOB[TI"; NSString *reflectionPool = @"bewmEwk|y[Dm@zAPEj@{AO"; GMSPolygon *hollowPolygon = [[GMSPolygon alloc] init]; hollowPolygon.path = [GMSPath pathFromEncodedPath:hydePark]; hollowPolygon.holes = @[[GMSPath pathFromEncodedPath:archibaldFountain], [GMSPath pathFromEncodedPath:reflectionPool]]; hollowPolygon.fillColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.2]; hollowPolygon.strokeColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0]; hollowPolygon.strokeWidth = 2; hollowPolygon.map = mapView;