The use of aerial anchors for localizing terrestrial nodes has recently been recognized as a cost-effective, swift and flexible solution for better localization accuracy, providing localization services when the GPS is jammed or satellite reception is not possible. In this paper, the localization of terrestrial nodes when using mobile unmanned aerial vehicles (UAVs) as aerial anchors is presented. We propose a novel framework to derive localization error in urban areas. In contrast to the existing works, our framework includes height-dependent UAV to ground channel characteristics and a highly detailed UAV energy consumption model. This enables us to explore different tradeoffs and optimize UAV trajectory for minimum localization error. In particular, we investigate the impact of UAV altitude, hovering time, number of waypoints and path length through formulating an energy-constrained optimization problem. Our results show that increasing the hovering time decreases the localization error considerably at the cost of a higher energy consumption. To keep the localization error below 100m, shorter hovering is only possible when the path altitude and radius are optimized. For a constant hovering time of 5 seconds, tuning both parameters to their optimal values brings the localization error from 150m down to 65m with a power saving around 25%