在Google Maps API v3上反转地理编码 [英] Reverse Geocode On Google Maps api v3

查看:131
本文介绍了在Google Maps API v3上反转地理编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怀疑是否有人可以帮助我。



我使用下面显示的代码正确绘制从Google Map上的MySQL数据库检索到的标记。

 < script type =text / javascript> 
// 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();
函数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
});
}
函数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`detectedlocations` 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 />'); \\\
);
}
mysql_close($ connect);
?>

center = bounds.getCenter();
map.fitBounds(bounds);
}
< / script>

我现在要做的是添加更多功能,允许用户点击地图绘制新的标记,实质上使用数据库中已有的标记作为工作的起点,执行反向地理编码



我已经研究了很多天了,我试着实现了大量的教程,但是我似乎无法让这两个功能都起作用。



我知道要启用点击事件,我需要将以下内容合并到一起:


$ b $ p $ lt; code> google.maps.event.addListener(map,'click',function(event){
marker.setPosition(event.latLng)
geocode_lookup('latLng',event.latLng);
});
}



但我必须承认我有点不确定我还需要合并。



我只是想知道是否有人可以看看这个请求,如果有人能告诉我我出错的地方,我会非常感激。

b
$ b

非常感谢和亲切的问候

解决方案

我写了一个单独的地图页面,反向地理编码功能

http://jsfiddle.net / ZDQeM /



我想,地址细节令人困惑。结果是一个数组,以不同的精度级别,可能包括县,另一个州,另一个街道地址。一般我只使用结果[0]。详细信息位于文档中: https://developers.google.com/maps/ documentation / javascript / geocoding#GeocodingResponses



如果您需要特定信息,获取它的肯定方式是遍历整个结果数组,直到找到所需的信息(例如包含postal_code的types [])。

  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(地理编码不成功:状态+状态);
}
});
});

您的代码在哪里?

 < script type =text / javascript> 
// 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();
函数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
});
}
函数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`detectedlocations` 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 />'); \\\
);
}
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(没有结果);
}
}
其他{
alert(Geocoding failed:Status+ status);
}
});
});
}
< / script>


I wonder whether someone may be able to help me please.

I using the code shown below to correctly plot markers retrieved from a MySQL database on a Google Map.

<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);
                        }
</script> 

What I'm now trying to do is add further functionality that allows users to also click on the map to plot new markers, in essence using the pre-existing marker from the database as a point to work from, performing a reverse geocode.

I've been researching this for a number of days now and I've tried to implement a whole host of tutorials, but I just can't seem to get both parts of the functionality working.

I do know that to enable a on-click event I need to incorporate something along the lines of:

google.maps.event.addListener(map, 'click', function(event) { marker.setPosition(event.latLng) geocode_lookup( 'latLng', event.latLng ); }); }

but I must admit I'm a little unsure about what else I need to incorporate.

I just wondered whether someone may be able to take a look at this please, and I'd be very grateful if someone could show me where I've gone wrong.

Many thanks and kind regards

解决方案

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> 

这篇关于在Google Maps API v3上反转地理编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆