www.webdeveloper.com
Results 1 to 7 of 7

Thread: google.maps.Geocoder problem

  1. #1
    Join Date
    Jun 2011
    Posts
    4

    google.maps.Geocoder problem

    This is my code:
    HTML Code:
    <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>
    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?

  2. #2
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    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="http://maps.google.com/maps/api/js?s...alse"></script> 
    <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>

  3. #3
    Join Date
    Jun 2011
    Posts
    4
    Is there a way to put that thing in a var and use it in some other place of my code?

  4. #4
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    Yes, absolutely. Can you clarify 'thing'? You mean the resulting formatted address or the whole function thing? How else do you wish to use?

  5. #5
    Join Date
    Jun 2011
    Posts
    4
    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
    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.

  6. #6
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    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:

    Code:
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?s...alse"></script> 
    <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) {
         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
     
    /*––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
    //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
    //..........whatever comes next.....
    //...........
    }//end of someFunction
     
    </script>
    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.

  7. #7
    Join Date
    Jun 2011
    Posts
    4
    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?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles