谷歌地图 - gmap v2 -> v3(自定义标记未显示+如何添加PHP地址)


google maps - gmap v2 -> v3 ( custom marker not showing + how to add the php address )

这是我的 v2 代码(标记未显示)

<body onload="load()"> 
<div id="map_canvas" style="width: 520px; height: 370px"></div> 
<script type="text/javascript"> 
var userLocation = '<?php echo $address; ?>';

if (GBrowserIsCompatible()) { var geocoder = new GClientGeocoder(); geocoder.getLocations(userLocation, function (locations) {
如果(位置。地标) { var north=位置。地标[0]。ExtendedData.LatLonBox.north; VAR 南部 = 位置。地标[0]。ExtendedData.LatLonBox.south; var east = 位置。地标[0]。ExtendedData.LatLonBox.east; var west = 位置。地标[0]。ExtendedData.LatLonBox.west;

     var bounds = new GLatLngBounds(new GLatLng(south, west), 
                                    new GLatLng(north, east));
     var map = new GMap2(document.getElementById("map_canvas"));
      var Icon = new GIcon();
      Icon.image = "images/422marker.png";
      Icon.iconSize = new GSize(33, 50);
     map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
     map.addOverlay(new GMarker(bounds.getCenter()), Icon);
  }

});}

这是我的 v3 代码(标记没有显示,不确定如何使用我们的 php 用户位置地址脚本)

<body onload="load()">
<div id="map_canvas" style="width: 520px; height: 370px"></div>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>

<script type="text/javascript">
var userLocation = '5th Avenue, New York';
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
  zoom: 8,
  center: latlng,
  mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': userLocation}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    // Geolocation was sucessfull
    // Set Marker Icon
    var icon = new google.maps.MarkerImage('images/422marker.png',
      new google.maps.Size(33,50),
      new google.maps.Point(0,0),
      new google.maps.Point(0,32));

    // Move map to position and set zoom
    map.setCenter(results[0].geometry.location);
    map.setZoom(11);
    var marker = new google.maps.Marker({
      map: map,
      position: results[0].geometry.location,
      title: userLocation
      //icon: icon
      });
  } else {
    alert("Geocode was not successful for the following reason: " + status);
  }
});

所以我遇到的问题是:

v2 :

自定义标记未显示,因此将地图更新为 v3

v3 :

自定义标记不显示+不确定如何使用我们的php代码将地图居中在我们的坐标上,从另一个脚本(在v2中工作)获取

任何帮助表示赞赏。

您只需

要将自定义图像的 url 传递给标记初始化中的图标键

var marker = new google.maps.Marker({
  map: map,
  position: results[0].geometry.location,
  title: 'userLocation',
  icon: 'images/422marker.png'
});