简体   繁体   English

如何使用地方网址创建谷歌地图标记

[英]How to create google maps markers with place url

I want to know if it is possible to create markers in google maps using a place URL like https://www.google.lk/maps/place/London instead of using latitude and longitude coordinates. 我想知道是否可以使用https://www.google.lk/maps/place/London等地点网址在Google地图中创建标记,而不是使用纬度和经度坐标。 I'm using the following code to create markers. 我正在使用以下代码来创建标记。

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);

You could utilize Places Library for that purpose, in particular textSearch method of google.maps.places.PlacesService object. 您可以将Places库用于此目的,特别是google.maps.places.PlacesService对象的textSearch方法。

Complete example 完整的例子

 function initialize() { var mapOptions = { mapTypeId: google.maps.MapTypeId.ROADMAP, zoom: 15 } var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); var request = { query: 'London' }; var service = new google.maps.places.PlacesService(map); service.textSearch(request, callback); function callback(results, status) { if (status == google.maps.places.PlacesServiceStatus.OK) { var marker = new google.maps.Marker({ map: map, place: { placeId: results[0].place_id, location: results[0].geometry.location } }); map.setCenter(results[0].geometry.location); } } } google.maps.event.addDomListener(window, 'load', initialize); 
  html, body, #map-canvas { height: 100%; margin: 0px; padding: 0px; } 
 <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=places"></script> <div id="map-canvas"></div> 

You may need to convert the place name → the tuple of longtitude and latitude with Google Map Geocoding service first as follows: 您可能需要首先使用Google Map Geocoding服务转换地名长度和纬度元组,如下所示:

  1. Request the Google Geocoding service via GET, pass the place name in (just a city name as your example used is OK): 通过GET请求Google地理编码服务,传递地名 (只是一个城市名称,因为您使用的示例是正常的):

    GET: https://maps.googleapis.com/maps/api/geocode/json?address=london GET: https//maps.googleapis.com/maps/api/geocode/json? address = london

    This returns you back a JSON containing the geospatial information about London: 这将返回包含有关伦敦的地理空间信息的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" : { ... ... 
    1. Given the JSON response, extract the "rough location" of that place in latitude, longitude coordinate as follows: 给定JSON响应,在纬度,经度坐标中提取该位置的“粗略位置” ,如下所示:

       var coord = response.results[0].geometry.location; 

    So you get: coord = {lat : 51.5073509, lng : -0.1277583} 所以你得到: coord = {lat : 51.5073509, lng : -0.1277583}

    1. Then you draw a marker at that given coordinate, and done. 然后在该给定坐标处绘制标记 ,并完成。

       var marker = new google.maps.Marker({ position: coord, // {lat : 51.5073509, lng : -0.1277583} map: map, title: 'London' }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM