Skip to content

Instantly share code, notes, and snippets.

@vkurchatkin
Last active December 15, 2015 05:48
Show Gist options
  • Save vkurchatkin/5211233 to your computer and use it in GitHub Desktop.
Save vkurchatkin/5211233 to your computer and use it in GitHub Desktop.
Generates code of MKPolygon from array of objects with lat and lng properties.
function renderToObjC(latlngs) {
var code = '',
i,
count = latlngs.length + 1;
code += 'coordinates = malloc(' + count + ' * sizeof(CLLocationCoordinate2D));\n';
for (i = 0; i < latlngs.length; i += 1) {
code += 'coordinates[';
code += i;
code += '] = CLLocationCoordinate2DMake(';
code += latlngs[i].lat;
code += ', ';
code += latlngs[i].lng;
code += ');\n';
}
code += 'coordinates[';
code += latlngs.length;
code += '] = CLLocationCoordinate2DMake(';
code += latlngs[0].lat;
code += ', ';
code += latlngs[0].lng;
code += ');\n';
code += 'polygon = [MKPolygon polygonWithCoordinates:coordinates count:';
code += latlngs.length;
code += '];\n';
code += 'free(coordinates);\n';
code += '[polygons addObject:polygon];';
return code;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment