I saw 3 things that popped up right away with your violin.
- You have never referenced Google javascript libraries.
- You never called
initialize()
and codeLatLng()
- You used the
value
property in the div element, but what you really wanted was the getAttribute()
method.
I changed your script so that it works now
For completeness, the source code was as follows.
<div id="latlng" value="54.9882,-1.5747"></div> <div id="test"></div> var geocoder; function initialize() { geocoder = new google.maps.Geocoder(); } function codeLatLng() { var input = document.getElementById("latlng").value; 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) { document.getElementById("test").innerHTML = '' + (results[4].formatted_address); + '' }); }
Work code:
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script> <div id="latlng" value="54.9882,-1.5747"></div> <div id="test"></div> var geocoder; initialize(); codeLatLng(); function initialize() { geocoder = new google.maps.Geocoder(); } function codeLatLng() { var input = document.getElementById("latlng").getAttribute('value'); console.log(input); 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) { document.getElementById("test").innerHTML = '' + (results[4].formatted_address); + '' }); }
Pete source share