Three things troubled King David greatly, they were the location of the altar for the temple, the incident with Uriah and the taunts of the Israelite tribes over his marriage to Batsheva. In David's eagerness to decapitate Goliath the Philistine giant, his helper Uriah the Hittite diverted David's divine ‘destiny’ by requesting and receiving a right to marry from among any of the Jewish women of Israel. This misappropriation caused King David tremendous suffering through a series of tragic events that ultimately led to the division of the Israelite nation.
Uriah used his grant to marry Batsheva and became a high ranking officer. Whilst Uriah was fighting Israel’s war, King David and Batsheva had a public and immoral, some say illicit, affair from which she conceived. To deceive the public David recalled Uriah from the war, but when Uriah refused to visit Batsheva David sent him back to the battlefront with a sealed letter instructing David’s General, Yoav, to put Uriah in harms way. In protest, Yoav leaked the letter. Uriah was killed and national confidence in the King waned as some tribes were repulsed by David's indiscretion. Batsheva bore David’s child who died after 7 days. This child was not the immaculate conception. In a way David saw the child’s death as a blessing and behaved accordingly. David had usurped God's plan by giving his marital right to Uriah who used it to marry Batsheva the one destined for the King. Though tarnished by their actions, David married Batsheva who became the mother of Solomon, King David’s ultimate heir and constructor of the nation’s first permanent temple.
Then, toward the end of King David’s life he did something very strange. Against Jewish law and the wishes of Yoav, his most loyal and senior general, the King ordered the army to take a census of the tribes in Israel's North and Yehuda in the South. The commentaries consider this lapse, a transgression of The Kings supreme providential confidence, but this time, in his old age, King David doubted the nation would continue to support him. Perhaps his insecurity developed through punishing events for his impetuous acts. It is said to have caused him to order the census, usually reserved for a holy purpose, but here it may have been to satisfy his nagging desire and to remind his restless nation of his status.
About 35 years earlier King David located the Ark of the Covenant in Jerusalem's City of David. Fragmented political circumstances made him abundantly aware he would not be the one to build the temple, that honor would be left for his son Solomon. Always on his mind King David planned every last detail and gathered materials for the temple construction. He was determined to be associated with the Ark’s final place of rest. Before David was anointed King over all the tribes he studied with Prophet Shmuel (Samuel) to discover where the Ark should be permanently located. Together they learned the book of Yehoshua, particularly the territorial boundary of Binyamin and Yehuda, the critical point that identifies the prescribed location of the altar as required for building the temple and by which the Ark obtains its relative position. He also studied with Prophet Nathan as he continued to plan the temples intricate details and he argued vehemently about its proper place with Do'eg Israel's greatest scholar. Throughout his tenure King David had been unable to deduce the Altar's place and therefore the Ark's ultimate location according to Jewish law.
As a result of the ill fated census, a series of unfortunate events presented King David the political opportunity to finalize the temple location. The nation was punished with severe drought during which the King repented for his actions and those of the nation. In order to release the nation from its desperate state Gad, the prophet and seer, advised the King to make a national choice:- Three years of famine, three months fleeing invading enemies or three days of plague? King David chose the latter and the plague ravaged 70,000 Israelites, but before the allotted time was up God stopped His angel of death as it was about to reign pestilence on the people of Jerusalem (Mount Zion and Mount Moriah - The City of David). The angel challenged God over His broken deal, in response God offered Avishai, Yoav’s brother, the King’s most loyal follower lost his life. In the moment of this national pandemonium, David begged God’s forgiveness and Gad advised the King to make an altar at the site the King had seen the angel was standing poised to destroy Jerusalem. The site belonged to the Jebusite King Ornan (Araunah) who lived in Jerusalem during David's entire reign, it was his threshing floor on the top of Mount Moriah.
Locating the altar for the temple was no simple matter. The laws are precisely established and every scholar of Israel’s tribes would have known and opined them. King David had struggled with many of the brightest scholars, including Do’eg his most vocal opponent, to determine its location. It is said David wanted to build in the middle of the mountain, but Do’eg ridiculed him for such thought. Any move by him to ‘force’ this decision would have been pounced on by the most senior tribal scholars and leaders who opposed his leadership. The end of the plague presented the sometimes apparently, impetuous King the perfect national moment to purchase the site, build an altar and make a burnt offering as a sacrifice for himself and perhaps on behalf of his confounded nation. In doing so and against the law of the day, he enabled the status of his private altar (bamah) to become communal thus establishing its location on the top of Mount Moriah.
The law of the temple altar location stipulates it as the site where Isaac was offered by Abraham as a sacrifice. No other location could bestow permanence deserving of God’s Home and indeed the first and second temples at the site Prophet Gad enabled and King David prescribed were eventually destroyed. Maimonides suggested that Moses wisely chose not to mention Jerusalem. Had he done so, the non-Jewish nations would have realized Jerusalem's singular importance to the Jewish people, and they would have fought fiercely to prevent it from falling into Israel's hands. Did King David know his alternative location would preserve the real site for the future?
King David knew of the prophecies that described and would cause the destruction of the first and second temples, did his deceptive actions follow those of Moses to defend the temples ultimate location? Perhaps they did and perhaps the altars true and permanent location will ultimately be revealed at this City of David site http://israelfact.blogspot.com.au/2013/03/a-hypothesis-for-mizbeach-of-akeida.html
Immediately after the reign of King Solomon who completed the first temple, the nation was plunged into massive division by the southern tribes of Yehuda under Rehavam and northern tribes of Israel under Yerovam. Any view the nation held of the permanent status of that temple was challenged and eventually twice soundly defeated.
To this very day orthodox Jews around the world say the Nefilat Appayim or tahannun section of their daily prayer of repentance, which was written by King David and recorded as Psalm 25. Here Jews acknowledge their repentance for the tragedy of King David's fateful decision, some begin the prayer "and David said to Gad" from Samuel II 24:14.
 Samuel 2 22:17,18 Yalkut Me’am Leoz
 Guide to the Perplexed III:45