我目前正在开发可显示标记和折线的模块,以显示机器人从位置A到位置B的路线。当涉及通过长触摸添加标记和线条时,它仅显示绘制的GMSPolylines和添加。但是,没有添加标记。请问您能否分别在两者之间绘制标记和多边形?
以下是我的代码
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
array = [[NSMutableArray alloc] init];
// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:22.2855200
longitude:114.1576900
zoom:12];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.delegate = self;
mapView_.myLocationEnabled = YES;
mapView_.settings.compassButton = YES;
mapView_.settings.myLocationButton = YES;
// mapView_.delegate = self;
self.view = mapView_;
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(22.2855200, 114.1576900);
marker.title = @"My place";
marker.snippet = @"HK";
marker.map = mapView_;
}
- (void)addMarkers
{
// [mapView_ clear];
if([array count] > 0){
GMSMutablePath *path = [GMSMutablePath path];
for (int i = 0; i < [array count]; i++) {
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(11.11 , 123.123);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.position = position;
marker.title = @"Desitnation";
NSString *tmpLat = [[NSString alloc] initWithFormat:@"%f", position.latitude];
NSString *tmpLong = [[NSString alloc] initWithFormat:@"%f", position.longitude];
marker.snippet = [NSString stringWithFormat:@"%@ %@", tmpLat,tmpLong];
UIColor *color;
CheckPoints *cp = [array objectAtIndex:i];
[path addLatitude:cp.getLatitude longitude:cp.getLongitude];
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.geodesic = YES;
polyline.strokeWidth = 10.f;
if (cp.getState ==1) {
// Green
color = [UIColor colorWithHue:.2 saturation:1.f brightness:1.f alpha:1.0f];
polyline.strokeColor = [UIColor greenColor];
} else {
color = [UIColor colorWithHue:1. saturation:1.f brightness:1.f alpha:1.0f];
polyline.strokeColor = [UIColor redColor];
}
marker.icon = [GMSMarker markerImageWithColor:color];
marker.map = mapView_;
polyline.map = mapView_;
}
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) mapView:(GMSMapView *)mapView didLongPressAtCoordinate:(CLLocationCoordinate2D)coordinate{
// GMSMarker *marker3 = [[GMSMarker alloc] init];
// marker3.position = coordinate;
// marker3.title = @"Desitnation";
// NSString *tmpLat = [[NSString alloc] initWithFormat:@"%f", coordinate.latitude];
// NSString *tmpLong = [[NSString alloc] initWithFormat:@"%f", coordinate.longitude];
// marker3.snippet = [NSString stringWithFormat:@"%@ %@", tmpLat,tmpLong];
//
// marker3.map = mapView_;
CheckPoints *myCar=[[CheckPoints alloc] init];
[myCar setState:0];
[myCar setLatitude:coordinate.latitude];
[myCar setLongitude:coordinate.longitude];
NSString* theTextValue = @"Desitnation";
[myCar setDesp:theTextValue];
NSLog( @"%d", myCar.getState );
NSLog( @"%f", myCar.getLatitude);
NSLog( @"%f", myCar.getLongitude );
NSLog( @"%@", myCar.getDesp );
[array addObject:myCar];
CheckPoints *lastChk = array.lastObject;
for (int i = 0; i < [array count]; i++) {
CheckPoints *current = [array objectAtIndex:i];
if(current.getLatitude != lastChk.getLatitude && current.getLongitude != lastChk.getLongitude){
[current setState:1];
NSString* previousTitle = [NSString stringWithFormat:@"%@%@", @"Checkpoint" ,[NSString stringWithFormat:@"%i", i]];
[current setDesp:previousTitle];
}
}
[self addMarkers];
[ToastView showToastInParentView:self.view withText:@"What a toast!" withDuaration:5.0];
}
@end
答案 0 :(得分:2)
看起来你要重复添加相同的标记......
for (int i = 0; i < [array count]; i++) {
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(11.11 , 123.123);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
......硬编码点。
答案 1 :(得分:2)
self.googleMapsView是谷歌地图视图。
示例:self.getDirections(&#34; 26.9211992,75.8185761&#34 ;, destination:&#34; 26.8472496,75.7691909&#34;,waypoints:[&#34; 26.8686811,75.7568383&#34;] ,travelMode:nil,completionHandler:nil)
let baseURLGeocode = "https://maps.googleapis.com/maps/api/geocode/json?"
let baseURLDirections = "https://maps.googleapis.com/maps/api/directions/json?"
var selectedRoute: Dictionary<NSObject, AnyObject>!
var overviewPolyline: Dictionary<NSObject, AnyObject>!
var originCoordinate: CLLocationCoordinate2D!
var destinationCoordinate: CLLocationCoordinate2D!
func getDirections(origin: String!, destination: String!, waypoints: Array<String>!, travelMode: AnyObject!, completionHandler: ((status: String, success: Bool) -> Void)?) {
if let originLocation = origin {
if let destinationLocation = destination {
var directionsURLString = baseURLDirections + "origin=" + originLocation + "&destination=" + destinationLocation
if let routeWaypoints = waypoints {
directionsURLString += "&waypoints=optimize:true"
for waypoint in routeWaypoints {
directionsURLString += "|" + waypoint
}
}
print(directionsURLString)
directionsURLString = directionsURLString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
let directionsURL = NSURL(string: directionsURLString)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let directionsData = NSData(contentsOfURL: directionsURL!)
do{
let dictionary: Dictionary<NSObject, AnyObject> = try NSJSONSerialization.JSONObjectWithData(directionsData!, options: NSJSONReadingOptions.MutableContainers) as! Dictionary<NSObject, AnyObject>
let status = dictionary["status"] as! String
if status == "OK" {
self.selectedRoute = (dictionary["routes"] as! Array<Dictionary<NSObject, AnyObject>>)[0]
self.overviewPolyline = self.selectedRoute["overview_polyline"] as! Dictionary<NSObject, AnyObject>
let legs = self.selectedRoute["legs"] as! Array<Dictionary<NSObject, AnyObject>>
let startLocationDictionary = legs[0]["start_location"] as! Dictionary<NSObject, AnyObject>
self.originCoordinate = CLLocationCoordinate2DMake(startLocationDictionary["lat"] as! Double, startLocationDictionary["lng"] as! Double)
let endLocationDictionary = legs[legs.count - 1]["end_location"] as! Dictionary<NSObject, AnyObject>
self.destinationCoordinate = CLLocationCoordinate2DMake(endLocationDictionary["lat"] as! Double, endLocationDictionary["lng"] as! Double)
let originAddress = legs[0]["start_address"] as! String
let destinationAddress = legs[legs.count - 1]["end_address"] as! String
let originMarker = GMSMarker(position: self.originCoordinate)
originMarker.map = self.googleMapsView
originMarker.icon = GMSMarker.markerImageWithColor(UIColor.greenColor())
originMarker.title = originAddress
let destinationMarker = GMSMarker(position: self.destinationCoordinate)
destinationMarker.map = self.googleMapsView
destinationMarker.icon = GMSMarker.markerImageWithColor(UIColor.redColor())
destinationMarker.title = destinationAddress
if waypoints != nil && waypoints.count > 0 {
for waypoint in waypoints {
let lat: Double = (waypoint.componentsSeparatedByString(",")[0] as NSString).doubleValue
let lng: Double = (waypoint.componentsSeparatedByString(",")[1] as NSString).doubleValue
let marker = GMSMarker(position: CLLocationCoordinate2DMake(lat, lng))
marker.map = self.googleMapsView
marker.icon = GMSMarker.markerImageWithColor(UIColor.purpleColor())
}
}
let route = self.overviewPolyline["points"] as! String
let path: GMSPath = GMSPath(fromEncodedPath: route)!
let routePolyline = GMSPolyline(path: path)
routePolyline.map = self.googleMapsView
}
else {
print("status")
//completionHandler(status: status, success: false)
}
}
catch {
print("catch")
// completionHandler(status: "", success: false)
}
})
}
else {
print("Destination is nil.")
//completionHandler(status: "Destination is nil.", success: false)
}
}
else {
print("Origin is nil")
//completionHandler(status: "Origin is nil", success: false)
}
}
答案 2 :(得分:1)
你永远不会使用路径点的坐标,而是使用硬编码的坐标:
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(11.11 , 123.123);
使用路径
CLLocationCoordinate2D position = [path coordinateAtIndex:i];