php - Reverse Geocode On Google Maps api v3 -
i wonder whether may able me please.
i using code shown below correctly plot markers retrieved mysql database on google map.
<script type="text/javascript"> //sample code written august li var icon = new google.maps.markerimage("images/location-marker-2.png") new google.maps.point(16, 32); var center = null; var map = null; var bounds = new google.maps.latlngbounds(); function addmarker(lat, lng, info) { var pt = new google.maps.latlng(lat, lng); bounds.extend(pt); var marker = new google.maps.marker({ position: pt, icon: icon, map: map }); } function initmap() { map = new google.maps.map(document.getelementbyid("gmaps-canvas"), { center: new google.maps.latlng(0, 0), zoom: 6, scrollwheel: true, draggable: true, maptypeid: google.maps.maptypeid.satellite }); <?php include("admin/link.php"); include("admin/opendb.php"); $query = mysql_query("select * `detectinglocations` `locationid` = '$lid'"); while ($row = mysql_fetch_array($query)){ $locationname=$row['locationname']; $osgb36lat=$row['osgb36lat']; $osgb36lon=$row['osgb36lon']; echo ("addmarker($osgb36lat, $osgb36lon,'<b>$locationname</b><br/>');\n"); } mysql_close($connect); ?> center = bounds.getcenter(); map.fitbounds(bounds); } </script> what i'm trying add further functionality allows users click on map plot new markers, in essence using pre-existing marker database point work from, performing reverse geocode.
i've been researching number of days , i've tried implement whole host of tutorials, can't seem both parts of functionality working.
i know enable on-click event need incorporate along lines of:
google.maps.event.addlistener(map, 'click', function(event) { marker.setposition(event.latlng) geocode_lookup( 'latlng', event.latlng ); }); }
but must admit i'm little unsure else need incorporate.
i wondered whether may able take @ please, , i'd grateful if show me i've gone wrong.
many , kind regards
i wrote separate maps page click-to-reverse-geocode functionality
the address details confusing work with, think. results array, @ different levels of precision, 1 might include county, state, street address. use results[0]. details in docs: https://developers.google.com/maps/documentation/javascript/geocoding#geocodingresponses
if need specific information sure way obtain iterate through whole results array until find need (types[] containing postal_code, example).
google.maps.event.addlistener(map, 'click', function(event) { usermarker = new google.maps.marker({ map: map, position: event.latlng }); geocoder.geocode({'latlng': event.latlng}, function(results, status) { if(status == google.maps.geocoderstatus.ok) { if (results[0]) { alert(results[0].formatted_address); } else { alert("no results"); } } else { alert("geocoding unsuccessful: status " + status); } }); }); where in code?
<script type="text/javascript"> //sample code written august li var icon = new google.maps.markerimage("images/location-marker-2.png") new google.maps.point(16, 32); var center = null; var map = null; var bounds = new google.maps.latlngbounds(); function addmarker(lat, lng, info) { var pt = new google.maps.latlng(lat, lng); bounds.extend(pt); var marker = new google.maps.marker({ position: pt, icon: icon, map: map }); } function initmap() { map = new google.maps.map(document.getelementbyid("gmaps-canvas"), { center: new google.maps.latlng(0, 0), zoom: 6, scrollwheel: true, draggable: true, maptypeid: google.maps.maptypeid.satellite }); <?php include("admin/link.php"); include("admin/opendb.php"); $query = mysql_query("select * `detectinglocations` `locationid` = '$lid'"); while ($row = mysql_fetch_array($query)){ $locationname=$row['locationname']; $osgb36lat=$row['osgb36lat']; $osgb36lon=$row['osgb36lon']; echo ("addmarker($osgb36lat, $osgb36lon,'<b>$locationname</b><br/>');\n"); } mysql_close($connect); ?> center = bounds.getcenter(); map.fitbounds(bounds); var geocoder = new google.maps.geocoder(); google.maps.event.addlistener(map, 'click', function(event) { var usermarker = new google.maps.marker({ map: map, position: event.latlng }); geocoder.geocode({'latlng': event.latlng}, function(results, status) { if(status == google.maps.geocoderstatus.ok) { if (results[0]) { alert(results[0].formatted_address); } else { alert("no results"); } } else { alert("geocoding unsuccessful: status " + status); } }); }); } </script>
Comments
Post a Comment