admin管理员组文章数量:1289834
I can update the google map marker position but I cannot update the google map position. It does not centres the map to the latitude and longitude position given
var myLatlng;
var map;
var infowindow;
var latitude;
var longtitude;
var marker;
loadMap();
function loadMap()
{
myLatlng = new google.maps.LatLng(54.91252, -1.37664);
var mapOptions = {
zoom: 17,
center: myLatlng
};
map = new google.maps.Map(document.getElementById("googlemaps"), mapOptions);
var contentString = '<h5>The Mew </h5>';
infowindow = new google.maps.InfoWindow({
content: contentString
});
marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: "The Mew",
animation: google.maps.Animation.DROP
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker);
});
infowindow.open(map, marker);
}
function updatePosition()
{
latitude = document.getElementById('latitude');
longtitude = document.getElementById('longtitude');
myLatlng = new google.maps.LatLng(latitude, longtitude);
marker.setPosition(myLatlng);
map.setCenter(myLatlng);
}
<input type="text" id="latitude" />
<input type="text" id="longtitude" />
<a onclick="updatePosition()" >update </a>
I can update the google map marker position but I cannot update the google map position. It does not centres the map to the latitude and longitude position given
var myLatlng;
var map;
var infowindow;
var latitude;
var longtitude;
var marker;
loadMap();
function loadMap()
{
myLatlng = new google.maps.LatLng(54.91252, -1.37664);
var mapOptions = {
zoom: 17,
center: myLatlng
};
map = new google.maps.Map(document.getElementById("googlemaps"), mapOptions);
var contentString = '<h5>The Mew </h5>';
infowindow = new google.maps.InfoWindow({
content: contentString
});
marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: "The Mew",
animation: google.maps.Animation.DROP
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker);
});
infowindow.open(map, marker);
}
function updatePosition()
{
latitude = document.getElementById('latitude');
longtitude = document.getElementById('longtitude');
myLatlng = new google.maps.LatLng(latitude, longtitude);
marker.setPosition(myLatlng);
map.setCenter(myLatlng);
}
<input type="text" id="latitude" />
<input type="text" id="longtitude" />
<a onclick="updatePosition()" >update </a>
Share
Improve this question
asked May 7, 2014 at 9:10
meWantToLearnmeWantToLearn
1,7002 gold badges25 silver badges47 bronze badges
3
- Possible duplicate of: stackoverflow./questions/2818984/… – edcs Commented May 7, 2014 at 9:16
- 1 @edcs plz read the question. They are different questions – meWantToLearn Commented May 7, 2014 at 9:18
-
Don't just call
loadMap
directly. Instead add a google event listener for the window to load, which then calls it:google.maps.event.addDomListener(window, 'load', loadMap);
– duncan Commented May 7, 2014 at 9:28
2 Answers
Reset to default 8In updatePosition
the information you're assigning to latitude and longitude are the DOM nodes of those input fields but you want to assign the value of those inputs instead. Also, you need to ensure that the text you're grabbing from those values is converted to a number for LatLng
to accept them properly. You can use parseInt
for that. Don't forget the radix.
function updatePosition() {
latitude = parseInt(document.getElementById('latitude').value, 10);
longtitude = parseInt(document.getElementById('longtitude').value, 10);
myLatlng = new google.maps.LatLng(latitude, longtitude);
marker.setPosition(myLatlng);
map.setCenter(myLatlng);
}
You can use jQuery to get values of inputs
function updatePosition()
{
var lat= $('#latitude').val();
var lng= $('#longtitude').val();
myLatlng = new google.maps.LatLng(lat,lng);
marker.setPosition(myLatlng);
map.setCenter(myLatlng);
}
and in Javascript you have to use parseInt which will parse string and return a integer
lat = parseInt(document.getElementById('latitude').value,10);
lng = parseInt(document.getElementById('longtitude').value,10);
FIDDLE DEMO
本文标签: update google map position via javascriptStack Overflow
版权声明:本文标题:update google map position via javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741458264a2379883.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论