Reverse Geocode On Google Maps api v3

余生颓废 提交于 2019-12-02 04:14:38

I wrote a separate maps page with just click-to-reverse-geocode functionality

http://jsfiddle.net/ZDQeM/

The address details are confusing to work with, I think. The results are an array, at different levels of precision, one might include the county, another the state, another the street address. Generally I only use results[0]. The details are in the docs: https://developers.google.com/maps/documentation/javascript/geocoding#GeocodingResponses

If you need specific information the sure way to obtain it is iterate through the whole results array until you find what you need (types[] containing postal_code, for 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 your code?

<script type="text/javascript">
         //Sample code written by 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 * FROM `detectinglocations` WHERE `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> 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!