Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript GooglePlacesAPI中的地名出错_Javascript_Google Maps_Google Places Api_Google Geocoding Api_Google Places - Fatal编程技术网

Javascript GooglePlacesAPI中的地名出错

Javascript GooglePlacesAPI中的地名出错,javascript,google-maps,google-places-api,google-geocoding-api,google-places,Javascript,Google Maps,Google Places Api,Google Geocoding Api,Google Places,我想找出地名,所以我所做的是:首先在谷歌地图上点击我正在从地理编码器找到地名。现在,我正在将place\u id传递到Places API的函数PlacesService。现在我从那里得到了成功的请求,它返回了地点的详细信息,但它的名字不是正确的名字。我得到的是街道的名字 下面是我的代码: <script> function initMap() { var myLatlng = new google.maps.LatLng(37.3132072, -121.9

我想找出地名,所以我所做的是:首先在谷歌地图上点击我正在从地理编码器找到地名。现在,我正在将place\u id传递到Places API的函数PlacesService。现在我从那里得到了成功的请求,它返回了地点的详细信息,但它的名字不是正确的名字。我得到的是街道的名字

下面是我的代码:

<script>
    function initMap() {
        var myLatlng = new google.maps.LatLng(37.3132072, -121.9334579);
        var myOptions = {
            zoom: 10,
            center: myLatlng
        }
        var map = new google.maps.Map(document.getElementById("map"),
            myOptions);
        var geocoder = new google.maps.Geocoder();

        google.maps.event
            .addListener(
                map,
                'click',
                function(event) {
                    geocoder
                        .geocode({
                                'latLng': event.latLng
                            },
                            function(results, status) {
                                if (status == google.maps.GeocoderStatus.OK) {
                                    if (results[0]) {
                                        getname(results[0].place_id);
                                    }

                                }
                            });
                });

        function getname(place_id) {
            var placesService = new google.maps.places.PlacesService(map);
            placesService.getDetails({
                placeId: place_id
            }, function(results, status) {
                alert("NAME: " + results.name);
            });
        }
    }
</script>

函数initMap(){
var mylatng=newgoogle.maps.LatLng(37.3132072,-121.9334579);
变量myOptions={
缩放:10,
中心:myLatlng
}
var map=new google.maps.map(document.getElementById(“map”),
肌肽);
var geocoder=new google.maps.geocoder();
google.maps.event
.addListener(
地图,
“点击”,
功能(事件){
地理编码
.地理编码({
“latLng”:event.latLng
},
功能(结果、状态){
if(status==google.maps.GeocoderStatus.OK){
如果(结果[0]){
getname(结果[0]。位置\u id);
}
}
});
});
函数getname(place\u id){
var placesService=新的google.maps.places.placesService(map);
placesService.getDetails({
placeId:place\u id
},功能(结果、状态){
警报(“名称:+results.NAME”);
});
}
}
当我运行此代码时,得到的结果如下:

如果你点击地图上的“图标”作为地点,你会得到一个(基本上是一个普通的MouseEvent,添加了一个
placeId
属性)

将该
placeId
用于请求,而不是地理编码器的返回值

google.maps.event.addListener(map,'click', function(event) {
  if (event.placeId) {
    getname(event.placeId)
  } else {
    geocoder.geocode({'latLng': event.latLng},
      function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          if (results[0]) {
            getname(results[0].place_id);
          }
        }
    });
  }
});

代码片段:

函数initMap(){
var mylatng=new google.maps.LatLng(37.329343,-121.863077);
变量myOptions={
缩放:15,
中心:myLatlng
}
var map=new google.maps.map(document.getElementById(“map”),
肌肽);
var geocoder=new google.maps.geocoder();
google.maps.event
.addListener(
地图,
“点击”,
功能(事件){
if(event.placeId){
getname(event.placeId)
}否则{
地理编码
.地理编码({
“latLng”:event.latLng
},
功能(结果、状态){
if(status==google.maps.GeocoderStatus.OK){
如果(结果[0]){
getname(结果[0]。位置\u id);
}
}
});
}
});
函数getname(place\u id){
var placesService=新的google.maps.places.placesService(map);
placesService.getDetails({
placeId:place\u id
},功能(结果、状态){
警报(“名称:+results.NAME”);
});
}
}
google.maps.event.addDomListener(窗口“加载”,initMap)
html,
身体,
#地图{
身高:100%;
宽度:100%;
边际:0px;
填充:0px
}

如果您单击地图上作为地点的“图标”,您将得到一个(基本上是一个添加了
placeId
属性的常规鼠标事件)

将该
placeId
用于请求,而不是地理编码器的返回值

google.maps.event.addListener(map,'click', function(event) {
  if (event.placeId) {
    getname(event.placeId)
  } else {
    geocoder.geocode({'latLng': event.latLng},
      function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          if (results[0]) {
            getname(results[0].place_id);
          }
        }
    });
  }
});

代码片段:

函数initMap(){
var mylatng=new google.maps.LatLng(37.329343,-121.863077);
变量myOptions={
缩放:15,
中心:myLatlng
}
var map=new google.maps.map(document.getElementById(“map”),
肌肽);
var geocoder=new google.maps.geocoder();
google.maps.event
.addListener(
地图,
“点击”,
功能(事件){
if(event.placeId){
getname(event.placeId)
}否则{
地理编码
.地理编码({
“latLng”:event.latLng
},
功能(结果、状态){
if(status==google.maps.GeocoderStatus.OK){
如果(结果[0]){
getname(结果[0]。位置\u id);
}
}
});
}
});
函数getname(place\u id){
var placesService=新的google.maps.places.placesService(map);
placesService.getDetails({
placeId:place\u id
},功能(结果、状态){
警报(“名称:+results.NAME”);
});
}
}
google.maps.event.addDomListener(窗口“加载”,initMap)
html,
身体,
#地图{
身高:100%;
宽度:100%;
边际:0px;
填充:0px
}