如何在Google自定义地图中添加“链接到当前位置”?

时间:2013-03-19 15:54:43

标签: google-maps

我正在制作游戏的自定义谷歌地图,一切进展顺利,但我很难为人们添加一个选项,供人们链接到地图上的当前部分。

例如:http://www.gta4.net/map/?lat=-19.775390625&lng=-7.91015625&z=5

这个函数是否存在于api中,或者这是一个自定义脚本? 如果是这样,我怎么能实现这个目标?

1 个答案:

答案 0 :(得分:0)

此功能在API中不存在,但很容易添加(未经测试):

 // If there are any parameters at eh end of the URL, they will be in  location.search
 // looking something like  "?marker=3"

 // skip the first character, we are not interested in the "?"
 var query = location.search.substring(1);

 // split the rest at each "&" character to give a list of  "argname=value"  pairs
 var pairs = query.split("&");
 for (var i=0; i<pairs.length; i++) {
   // break each pair at the first "=" to obtain the argname and value
   var pos = pairs[i].indexOf("=");
   var argname = pairs[i].substring(0,pos).toLowerCase();
   var value = pairs[i].substring(pos+1).toLowerCase();

   // process each possible argname  -  use unescape() if theres any chance of spaces
   if (argname == "id") {id = unescape(value);}
   if (argname == "marker") {index = parseFloat(value);}
   if (argname == "lat") {lat = parseFloat(value);}
   if (argname == "lng") {lng = parseFloat(value);}
   if (argname == "zoom") {zoom = parseInt(value);}
   if (argname == "type") {
     // from the v3 documentation 8/24/2010
     // HYBRID This map type displays a transparent layer of major streets on satellite images. 
     // ROADMAP This map type displays a normal street map. 
     // SATELLITE This map type displays satellite images. 
     // TERRAIN This map type displays maps with physical features such as terrain and vegetation. 
     if (value == "m") {maptype = google.maps.MapTypeId.ROADMAP;}
     if (value == "k") {maptype = google.maps.MapTypeId.SATELLITE;}
     if (value == "h") {maptype = google.maps.MapTypeId.HYBRID;}
     if (value == "t") {maptype = google.maps.MapTypeId.TERRAIN;}

   }
 }

// create the map
  var myOptions = {
    zoom: zoom,
    center: new google.maps.LatLng(lat,lng),
    mapTypeId: maptype,
    mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
    navigationControl: true,
    mapTypeId: maptype
}
map = new google.maps.Map(document.getElementById("map_canvas"),
                                myOptions);

working example(移植自this page of Mike Williams' v2 tutorial上的示例)