使用select动态更改Google地图脚本src [英] Dynamically change google maps script src with select

查看:106
本文介绍了使用select动态更改Google地图脚本src的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击某个下拉选项时,我想更改Google地图脚本src,因为在我的网页上搜索邮政编码时遇到问题。为了获得正确的结果,我相信我需要在src网址中使用Google的Region Localization& region =( https://developers.google.com/maps/documentation/javascript/basics#Localization



我已经在< head> 标记中加载了我的Google Maps API:

<脚本type =text / javascriptsrc =http://maps.google.com/maps/api/js?sensor=false>< / script>



我创建了一个下拉列表

 < select id =country> ; 
< option value =sel>选择< / option>
< option value =DK>丹麦< / option>
< option value =NL>荷兰< / option>
< option value =SE>瑞典< / option>
< / select>

只要点击其中一个选项,我就会更改google地图脚本src。



例如:如果我点击丹麦,则src会变为:
http://maps.googleapis.com/maps/ api / js?sensor = false& region = DK



我通过浏览
但我仍然不能



现在我有这个:

  e.preventDefault(); 
var region = $(this).attr('value');
var url ='http://maps.googleapis.com/maps/api/js?sensor=false&region='+ region;
});

当谈到jQuery和JavaScript时,我真的很新鲜,所以我想也许我可以得到一些帮助在这里。

解决方案

你不应该这样做,因为删除脚本元素不会删除方法,变量等已经由脚本创建。您将收到如下警告:



警告:您已在此页面上多次包含Google Maps API。这可能会导致意外错误



相反,您应该在请求中使用region-parameter,例如当进行地理编码时


I want to change the Google maps script src when one of my dropdown options is clicked because I'm having an issue when searching for zip codes on my page. To get the correct result I believe I need to use Google's Region Localization "&region=" in the src url.(https://developers.google.com/maps/documentation/javascript/basics#Localization)

I've loaded my google maps api within the <head> tag:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>

I've created a drop down list

<select id="country">  
   <option value="sel">Select</option>
   <option value="DK">Denmark</option>
   <option value="NL">Netherlands</option>
   <option value="SE">Sweden</option>
</select>

I want the google maps script src to change whenever one of the options is clicked on.

For example: If I click on Denmark, the src would then change to: http://maps.googleapis.com/maps/api/js?sensor=false&region=DK

I've tried some stuff out by looking through this Dynamically change the src of a script tag but I still can't figure out how to make it work.

At the moment I have this:

$('#country option').click(function(e) {
    e.preventDefault();
    var region = $(this).attr('value');
    var url = 'http://maps.googleapis.com/maps/api/js?sensor=false&region=' + region;
});

I'm really newbie when it comes to jQuery and javascript so I thought maybe I could get some help in here.

解决方案

You shouldn't do it like that, because removing a script-element doesn't remove the methods,variables etc. that have been created by the script. You will receive a warning like that:

Warning: you have included the Google Maps API multiple times on this page. This may cause unexpected errors

Instead you should use the region-parameter inside the requests, e.g. when geocoding

这篇关于使用select动态更改Google地图脚本src的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆