How did nature influence Whitman to write the poem "Song of Myself"?
As a Transcendentalist writer, Walt Whitman believed in the idea (akin to the Unitarian belief today) that God, nature, and humans are all connected. "Song of Myself," as a long poem comprised of fifty-two shorter vignettes, was no exception to this belief.
For example, in VI., "A Child Said, "What Is the Grass?"", Whitman describes, in so many words, that grass is a representation of the cycle of life and death. He claims that the grass is "...very dark to be from the white heads of old mothers" and that it is very possible that the grass comes from "...the breasts of young men" (both quotes suggesting life), yet he also claims that grass seems to him "...the beautiful uncut hair of graves" (a true statement, both literally and figuratively). As Robin Williams claims, playing John Keating in the film Dead Poets Society, "we are food for worms." The ashes of the dead provide growth for new life.
It is no accident that "Song of Myself" is included in Whitman's most famous compilation of poetry, Leaves of Grass; after all, he claims later in XXXI., "I believe a leaf of grass is no less than the journey work of the stars," further solidifying his place as a Transcendentalist poet. Judging by the type of language he uses throughout the poem, Whitman reveres all that God has created, including the stars ("I hear you whispering there O stars of heaven") (XLIX). Though he did not retreat into nature for as extended periods of time as his contemporaries Emerson and Thoreau, Whitman did, quite obviously, celebrate himself as a part of a brilliant masterpiece in nature.
Finally, it is not just the grass and the stars that Whitman celebrates in "Song of Myself." He revels in the fact that he coexists with ALL creation, including animals (see XXXIII, where Whitman, as narrator, is almost in a trance-like state, noting ALL life around him through a series of parallel statements beginning with "Where" and "Over"). Also, earlier, in XXXII, Whitman claims he "think[s] [he] could live with animals, they are so placid and self-contain'd," continuing to make the assertion that overall, animals are much more content with their lives than humans are with their own.
One of many over-arching themes in "Song of Myself" is that we all have something to learn from nature, in part because we belong to it. Whitman, in this poem, boldly asserts that he is both in and of the world, that he has fully immersed himself in nature and therefore understands the world -- and himself -- better for it.