Hugh's Blog

谷歌地图绘制多个地点

官方就有很好的例子,具体开发还要找官方 api 文档,下面是一个简单的显示多个地点的例子。

<!DOCTYPE html>
<html> 
<head> 
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
  <title>Google Maps Multiple Markers</title> 
  <script src="http://maps.google.com/maps/api/js?sensor=false" 
          type="text/javascript"></script>
</head> 
<body>
  <div id="map" style="width: 500px; height: 400px;"></div>

  <script type="text/javascript">
    var locations = [
      ['Bondi Beach', -33.890542, 151.274856, 4],
      ['Coogee Beach', -33.923036, 151.259052, 5],
      ['Cronulla Beach', -34.028249, 151.157507, 3],
      ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
      ['Maroubra Beach', -33.950198, 151.259302, 1]
    ];

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 10,
      center: new google.maps.LatLng(-33.92, 151.25),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();

    var marker, i;

    for (i = 0; i < locations.length; i++) {  
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map
      });

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][0]);
          infowindow.open(map, marker);
        }
      })(marker, i));
    }
  </script>
</body>
</html>

2017-09-12

使用谷歌地图保存地址经纬度等信息。

首先导入 js 文件 http://maps.google.com/maps/api/js?&language=zh-TW&region=HK&key={YOUR_KEY}, 另外需要开启 Google Maps JavaScript APIGoogle Maps Geocoding API 访问权限。

// Google map start
var map = new google.maps.Map(document.getElementById('gmap'), {
    zoom: 10,
    center: new google.maps.LatLng(22.396428, 114.109497),
    mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker({
    position: new google.maps.LatLng(22.396428, 114.109497),
    map: map
});
var geocoder = new google.maps.Geocoder;

google.maps.event.addListener(map, 'click', function(event) {
    placeMarker(event.latLng);
    geocodeLatLng(geocoder, map, event.latLng);
});

function placeMarker(location) {
    if (marker) {
        marker.setPosition(location);
    } else {
        marker = new google.maps.Marker({
            position: location,
            map: map
        });
    }
    $('#input-address-latlng').val(location.lat() + ',' + location.lng());
}

function geocodeLatLng(geocoder, map, latLngObj) {
  var latlng = {lat: latLngObj.lat(), lng: latLngObj.lng()};
  geocoder.geocode({'location': latlng}, function(results, status) {
    var title = '';
    if (status === 'OK') {
        if (results[0]) {
            title = results[0].formatted_address;
        } else {
            title = 'No results found';
        }
    } else {
        title = 'Geocoder failed due to: ' + status;
    }
    $('#input-address-title').val(title);
  });
}
// Google map end

参考

Google Maps JS API v3 - Simple Multiple Marker Example

Google Maps JavaScript API

Reverse Geocoding