简体   繁体   English

Google Maps JS API v3-具有地理位置的简单多个标记

[英]Google Maps JS API v3 - Simple Multiple Marker with Geolocation

Google Maps JS API v3 - Simple Multiple Marker with Geolocation Google Maps JS API v3-具有地理位置的简单多个标记
Thanks, but I manage with the code below. 谢谢,但是我用下面的代码来管理。 Fell free to improve it! 随意改进吧!
1 Part - clean variables 1部分 -清洁变量

var LocationCenter = null;
var map = null;
var approxCircle = null;
var pinCircle = null;
var marker = null;

2 Part - define locations in Variable 2部分 -在变量中定义位置

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

3 Part - initialize function 3部分 -初始化功能

function Newinitialize(lat,lng) {
    LocationCenter = new google.maps.LatLng(lat,lng);
    var myOptions = {
        zoom: 14,
        center: LocationCenter,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
map = new google.maps.Map(document.getElementById("map_canvasRoast"), myOptions);

marker = new google.maps.Marker({
    position: LocationCenter,
    map: map,
    animation: google.maps.Animation.DROP
    });

approxCircle = {
  strokeColor: "#008595",
  strokeOpacity: 0.8,
  strokeWeight: 2,
  fillColor: "#008595",
  fillOpacity: 0.25,
  map: map,
  center: LocationCenter,
  radius: 50,
  clickable : false 
};

pinCircle = new google.maps.Circle(approxCircle);
var infowindow = new google.maps.InfoWindow();
var marker, i; 

4 Part - set locations 4部分 -设置位置

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

};

$('.goMap').live('click', function() {
    if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position){
        Newinitialize(position.coords.latitude,position.coords.longitude);
    });
    }else{
        Newinitialize(52.636161,-1.133065);
    }
});

Done, now I load the page and the geolocation is working fine. 完成,现在我加载了页面,地理位置正常。

Setup the Google Map inside the showPosition function, something like: showPosition函数中设置Google Map,例如:

var map;
function showPosition(position) {
  var latlon=position.coords.latitude+","+position.coords.longitude;

  x.innerHTML="Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;    

  map = new google.maps.Map(document.getElementById('map'), { 
    zoom: 10, 
    center: new google.maps.LatLng(position.coords.latitude, position.coords.longitude), 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
  });
}

This way the coordinates will be available before you initialize Google Maps. 这样,在初始化Google地图之前,坐标将可用。 Otherwise you could update the map coordinates within your showPosition function (I'm not sure of the exact code to do that). 否则,您可以在showPosition函数中更新地图坐标(我不确定执行此操作的确切代码)。

Here's how you would re-center the map to the user's current location: 这是将地图重新​​居中到用户当前位置的方法:

function showPosition(position)
{
  map.setCenter(new google.maps.LatLng(positions.coords.latitude,
      position.coords.longitude));
}

This would allow you to create the map before receiving the user location. 这将允许您在接收用户位置之前创建地图。

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

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