Google地图:在下拉菜单中更改地图标记位置 [英] Google Maps : change map marker location on dropdown
本文介绍了Google地图:在下拉菜单中更改地图标记位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要根据下拉变化来更改地图标记的位置,我正在做的是我在latdown事件中获取经纬度,并希望将这些坐标传递给我当前的标记,这是我的代码
$ b $
$(#location)。change(function(){
var addr =($('#location'))。 val());
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address':addr},function(results,status){
if(status == google.maps.GeocoderStatus.OK){
alert(location:+ results [0] .geometry.location.lat()++ results [0] .geometry。 location.lng());
geoMarker.setMarkerOptions({
duration:duration,
easing:$('#easingOption')。val()
});
$ else {
alert(Something wrong wrong+ status);
}
});
});
HTML:
< select id =location>
< option>迪拜< / option>
< option> Sharjah< / option>
< / select>
它提示当前坐标,但我需要知道如何将这些坐标传递到我的标记位置 p>
解决方案
您需要根据地理编码操作的结果设置标记的位置。
$(#location)。change(function(){
var addr =($('#location')。val());
$ b $ var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address':addr
},function(results,status){
(if status == google.maps.GeocoderStatus.OK){
map.setCenter(results [0] .geometry.location);
geoMarker.setPosition(results [0] .geometry.location) ;
} else {
alert(Something wrong wrong+ status);
}
});
});
程式码片段:
var geocoder; var map; var geoMarker;函数initialize(){var map = new google.maps.Map(document.getElementById(map_canvas),{center:new google.maps。 LatLng(37.4419,-122.1419),zoom:13,mapTypeId:google.maps.MapTypeId.ROADMAP}); geoMarker = new google.maps.Marker(); geoMarker.setPosition(map.getCenter()); geoMarker.setMap(地图); $(#location)。change(function(){var addr =($('#location')。val()); var geocoder = new google.maps.Geocoder(); geocoder.geocode({'address ':addr},function(results,status){if(status == google.maps.GeocoderStatus.OK){map.setCenter(results [0] .geometry.location); geoMarker.setPosition(results [0] .geometry .location);} else {alert(Something got wrong+ status);}});});} google.maps.event.addDomListener(window,load,initialize);
html,body,#map_canvas {height:100%;宽度:100%; margin:0px;
< script src =https://
I want to change map marker position on basis of dropdown change even,What I'm doing is I get lat,long on dropdown event and want to pass these coordinates to my current marker , this is my code
$("#location").change(function () {
var addr = ($('#location').val());
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': addr }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
alert("location : " + results[0].geometry.location.lat() + " " + results[0].geometry.location.lng());
geoMarker.setMarkerOptions({
duration: duration,
easing: $('#easingOption').val()
});
} else {
alert("Something got wrong " + status);
}
});
});
HTML :
<select id="location">
<option>Dubai</option>
<option>Sharjah</option>
</select>
It alerts current coordinates but I need to know how do I pass these coordinates to my marker position
解决方案
You need to set the position of the marker based on the results of the geocode operation.
$("#location").change(function() {
var addr = ($('#location').val());
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address': addr
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
geoMarker.setPosition(results[0].geometry.location);
} else {
alert("Something got wrong " + status);
}
});
});
code snippet:
var geocoder;
var map;
var geoMarker;
function initialize() {
var map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(37.4419, -122.1419),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
geoMarker = new google.maps.Marker();
geoMarker.setPosition(map.getCenter());
geoMarker.setMap(map);
$("#location").change(function() {
var addr = ($('#location').val());
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address': addr
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
geoMarker.setPosition(results[0].geometry.location);
} else {
alert("Something got wrong " + status);
}
});
});
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<select id="location">
<option>Dubai</option>
<option>Sharjah</option>
</select>
<div id="map_canvas"></div>
这篇关于Google地图:在下拉菜单中更改地图标记位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文