Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.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 如何使用PlaceURL创建google地图标记_Javascript_Google Maps_Google Maps Api 3_Google Maps Markers - Fatal编程技术网

Javascript 如何使用PlaceURL创建google地图标记

Javascript 如何使用PlaceURL创建google地图标记,javascript,google-maps,google-maps-api-3,google-maps-markers,Javascript,Google Maps,Google Maps Api 3,Google Maps Markers,我想知道是否有可能在谷歌地图中创建标记,使用一个像这样的地方URL,而不是使用纬度和经度坐标。我正在使用以下代码创建标记 function initialize() { var myLatlng = new google.maps.LatLng(-25.363882,131.044922); var mapOptions = { zoom: 4, center: myLatlng } var map = new google.maps.Map(document.g

我想知道是否有可能在谷歌地图中创建标记,使用一个像这样的地方URL,而不是使用纬度和经度坐标。我正在使用以下代码创建标记

function initialize() {
  var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
  var mapOptions = {
    zoom: 4,
    center: myLatlng
  }
  var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

  var marker = new google.maps.Marker({
      position: myLatlng,
      map: map,
      title: 'Hello World!'
  });
}

google.maps.event.addDomListener(window, 'load', initialize);

您可能需要转换地名谷歌地图地理编码服务的长纬度元组首先如下所示:

var coord = response.results[0].geometry.location;
  • 通过GET请求谷歌地理编码服务,在中传递地名(仅使用城市名称即可):

    获取:

    这将返回一个JSON,其中包含关于伦敦的地理空间信息:

    {
      "results" : [
      {
         "address_components" : [
            {
               "long_name" : "ลอนดอน",
               "short_name" : "ลอนดอน",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "สหราชอาณาจักร",
               "short_name" : "GB",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "ลอนดอน สหราชอาณาจักร",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 51.6723432,
                  "lng" : 0.148271
               },
               "southwest" : {
                  "lat" : 51.38494009999999,
                  "lng" : -0.3514683
               }
            },
            "location" : {
               "lat" : 51.5073509,
               "lng" : -0.1277583
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
                  ...
                  ...
    
  • 给定JSON响应,提取该位置在经纬度坐标中的“大致位置”,如下所示:

    var coord = response.results[0].geometry.location;
    
  • 所以你得到:
    coord
    =
    {lat:51.5073509,lng:-0.1277583}

  • 然后在给定坐标处绘制一个标记,并完成

    var marker = new google.maps.Marker({
        position: coord, // {lat : 51.5073509, lng : -0.1277583}
        map: map,
        title: 'London'
    });
    
  • 您可以为此目的使用
    textSearch
    方法,尤其是
    google.maps.places.PlacesService
    对象

    完整示例

    函数初始化(){
    变量映射选项={
    mapTypeId:google.maps.mapTypeId.ROADMAP,
    缩放:15
    }
    var map=new google.maps.map(document.getElementById('map-canvas'),mapOptions);
    var请求={
    问题:“伦敦”
    };
    var service=newgoogle.maps.places.PlacesService(地图);
    文本搜索(请求、回调);
    函数回调(结果、状态){
    if(status==google.maps.places.PlacesServiceStatus.OK){
    var marker=new google.maps.marker({
    地图:地图,
    地点:{
    placeId:结果[0]。place\u id,
    位置:结果[0]。几何体。位置
    }
    });
    map.setCenter(结果[0].geometry.location);
    }
    }
    }
    google.maps.event.addDomListener(窗口“加载”,初始化)
    
    html,主体,#地图画布{
    身高:100%;
    边际:0px;
    填充:0px;
    }

    你说的“使用地点URL”是什么意思?那是从哪里来的?或者你指的是使用像“伦敦”这样的“地点”?嗨,我在问题中提供了一个地点URL的例子。您可以在此处阅读有关放置URL的更多信息。谢谢