0

我创建了一张关注用户位置的地图。该地图有一个带有信息窗口的标记。当用户单击信息窗口时,它会给他一个指向信息页面 (info.html) 的超链接。我想创建一个选项,允许用户从信息页面返回到地图,并且地图应该集中在同一个标​​记上(而不是他当前的位置)。它基本上是相反的。这听起来很简单,但我不知道如何编码。我想我可以为每个标记构建不同的地图,但这似乎不太可能是正确的解决方案。任何帮助,将不胜感激。

这是我对脚本的尝试,(initialize2()不起作用):

$(document).ready(function() {
    getLocation();
});

var map;
var jones = new google.maps.LatLng(40.59622788325198, -73.50334167480469);

function getLocation() {
    navigator.geolocation.getCurrentPosition(
        function(position) {
            var myLatLng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
            map.setCenter(myLatLng);   
        },
        function() {
            alert("Please enable location detection on your device");
        }
    );
}

function initialize() {
    var mapOptions = {
        center: map,
        zoom: 9,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

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

    var marker1 = new google.maps.Marker({
        position: jones,
        map: map
    });

    google.maps.event.addListener(marker1, 'click', function() {
        infowindow.setContent('<h4>Jones Beach</h4><a href="info.html">See info</a>');
        infowindow.open(map,marker1);   
    });
}

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

// this is my attempt to focus on the marker
// there will be a button that will evoke this function from info.html
function initialize2() {
  var mapOptions2 = {
    zoom: 14,
    center: new google.maps.LatLng(40.59622788325198, -73.50334167480469),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions2);
}

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

1 回答 1

2

在 url 中使用哈希怎么样?

尝试使用“ http://students.example.com/test.html#one ”:

$(function() {

    var places = {
        one: [40.59622788325198, -73.50334167480469],
        two: [50.59622788325198, -71.50334167480469]
    };

    var div = $('#map-canvas');

    var map = new google.maps.Map(div.get(0), {
        center: map,
        zoom: 9,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var markers = {};

    $.each(places, function(name) {

        markers[name] = new google.maps.Marker({
            position: new google.maps.LatLng(this[0], this[1]),
            map: map
        });

    });

    var place = location.hash.substr(1);

    if(place in places) {

        map.setCenter(markers[place].getPosition());

    }

});
于 2013-04-28T19:12:17.730 回答