/    Sign up×
Community /Pin to ProfileBookmark

google.maps.Geocoder problem

This is my code:

[code=html]
<script type=”text/javascript” src=”http://maps.google.com/maps/api/js?sensor=false”></script>
<script type=”text/javascript”>
var geocoder;
var loc;
geocoder = new google.maps.Geocoder();
var input = “32.83036,34.974339”
var latlngStr = input.split(“,”,2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({‘latLng’: latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
document.write(results[1].formatted_address);
}
});
</script>
[/code]

It supposed to print on the page just one line – city and country by LAT&LNG.
It doesn’t work. If I replace the “document.write” with “alert” it does work.
What went wrong?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@astupidnameJun 28.2011 — You may not use document.write after the page has loaded, only while it is still in process of loading. So you should not use it in the callback function passed to geocoder.geocode.

Solution below:

[CODE]<script type="text/javascript" src="[URL]http://maps.google.com/maps/api/js?sensor=false"></script[/URL]>
<script type="text/javascript">
//wait until the window has loaded and the document's elements are available
google.maps.event.addDomListener(window, 'load', function () {
var geocoder;
var loc;
geocoder = new google.maps.Geocoder();
var input = "32.83036,34.974339"
var latlngStr = input.split(",",2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
//show results in a DOM element,
//in this case a div with id attribute of 'someId'
document.getElementById('someId').innerHTML = results[1].formatted_address;
}
});
});
</script>

....

<div id="someId"></div>[/CODE]
Copy linkTweet thisAlerts:
@yuvalsauthorJun 28.2011 — Is there a way to put that thing in a var and use it in some other place of my code?
Copy linkTweet thisAlerts:
@astupidnameJun 28.2011 — Yes, absolutely. Can you clarify 'thing'? You mean the resulting formatted address or the whole function thing? How else do you wish to use?
Copy linkTweet thisAlerts:
@yuvalsauthorJun 28.2011 — [CODE]
var locationCode="NY, US" //see instructions above

// Set to 'false' if you'd prefer Farenheit
var isCelsius = true //true|false

// Use 'Real Feel' temperatures where possible, taking into account Wind Chill, Humidity etc.
var useRealFeel = true //true|false

/*––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
//Enable Wallpaper and/or Lock Screen
var enableWallpaper = true; //true|false
var enableLockScreen = false; //true|false [Currently, it is suggested that the lockScreen is disabled.]

// Supplied styles are 'originalBubble', 'myopia', 'iconOnly' and 'split'.
// (Add your own to the CSS folder!)
var stylesheetWall = 'topright' //'originalBubble'|'myopia'|'iconOnly'|'split'|'oneLine'
var stylesheetLock = 'iconOnly' //See above.

// The only supplied icon set is 'klear'
// Images must follow the same naming schema as the 'springboard' set (borrowed from KWeather)
var iconSetWall = 'springboard' //'springboard'|'Lockscreen'
var iconExtWall = ".png" //'.png'|.'gif' etc.
var iconSetLock = 'springboard' //See above.
var iconExtLock = '.png' //See above.


/*––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/

// The other available source is 'yahooWeather' which for the 'locale'
// requires a US zip or location code (e.g. UKXX0085 or CHXX0008) from http://weather.yahoo.com
var source = 'appleAccuweatherStolen' //'appleAccuweatherStolen'|'yahooWeather'

// Please endeavour to set this to a sensible value if you really must change it...
var updateInterval = 35 //Minutes[/CODE]


This is a JS file that I use in my iPhone for a weather widget.

What I'm really trying to do is to get the weather from a specific location from my gps coordinates.

The script from my first question translate latitude & longitude to a city and a country.

I need to place the result from that script in the first var of the above script - locationCode.

from searching the web I couldn't figure how to do this because i can't make it run before the rest of the code.
Copy linkTweet thisAlerts:
@astupidnameJun 28.2011 — Well, me being a simpleton with a fairly basic non-smart phone, I have no idea if you can actually make this work or not. Don't know if you will be able to use google maps or not, but if it were in a browser I would be doing something like:

&lt;script type="text/javascript" src="[URL]http://maps.google.com/maps/api/js?sensor=false"&gt;&lt;/script[/URL]&gt;
&lt;script type="text/javascript"&gt;
//wait until the window has loaded and the document's elements are available
google.maps.event.addDomListener(window, 'load', function () {
var geocoder;
var loc;
geocoder = new google.maps.Geocoder();
var input = "32.83036,34.974339"
var latlngStr = input.split(",",2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
someFunction(results[1].formatted_address);
}
});
});

function someFunction(locationCode) {
//locationCode now passed in as an argument to this function

// Set to 'false' if you'd prefer Farenheit
var isCelsius = true //true|false

// Use 'Real Feel' temperatures where possible, taking into account Wind Chill, Humidity etc.
var useRealFeel = true //true|false

/*&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;*/
//Enable Wallpaper and/or Lock Screen
var enableWallpaper = true; //true|false
var enableLockScreen = false; //true|false [Currently, it is suggested that the lockScreen is disabled.]

// Supplied styles are 'originalBubble', 'myopia', 'iconOnly' and 'split'.
// (Add your own to the CSS folder!)
var stylesheetWall = 'topright' //'originalBubble'|'myopia'|'iconOnly'|'split'|'oneLine'
var stylesheetLock = 'iconOnly' //See above.

// The only supplied icon set is 'klear'
// Images must follow the same naming schema as the 'springboard' set (borrowed from KWeather)
var iconSetWall = 'springboard' //'springboard'|'Lockscreen'
var iconExtWall = ".png" //'.png'|.'gif' etc.
var iconSetLock = 'springboard' //See above.
var iconExtLock = '.png' //See above.


/*&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;&amp;#8211;*/

// The other available source is 'yahooWeather' which for the 'locale'
// requires a US zip or location code (e.g. UKXX0085 or CHXX0008) from [URL]http://weather.yahoo.com[/URL]
var source = 'appleAccuweatherStolen' //'appleAccuweatherStolen'|'yahooWeather'

// Please endeavour to set this to a sensible value if you really must change it...
var updateInterval = 35 //Minutes
//..........whatever comes next.....
//...........
}//end of someFunction

&lt;/script&gt;

I'd be surprised if that's not bizarre and unworkable, as I say having no experience with iphone or how to do these things in it. Perhaps someone else may have a clue and pitch in.
Copy linkTweet thisAlerts:
@yuvalsauthorJun 28.2011 — thanks for trying, but it doesn't work.

Is there a way not to put the rest of the code in a function, just some how force the google maps script run before everything else and put the result on a var that can be used later?
×

Success!

Help @yuvals spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...