最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Google Map javascript api key - Stack Overflow

programmeradmin5浏览0评论

I am using a cordova application and have a browser key for map.

<script async defer

        src=";callback=initMap">
</script>

but I want to take my key from server , I took and I store this key as

localStorage.getItem("MapCode")

this locastorage gives

";callback=initMap"

so I want to write this to src but I couldn't do it.

<script async defer

            src=localStorage.getItem("MapCode")>
    </script>

How can I solve this?Thanks in advance

I am using a cordova application and have a browser key for map.

<script async defer

        src="https://maps.googleapis./maps/api/js?key=AIzaSyBHJxzPHD_egYnhxntqcvfem35YRjruzAg&callback=initMap">
</script>

but I want to take my key from server , I took and I store this key as

localStorage.getItem("MapCode")

this locastorage gives

"https://maps.googleapis./maps/api/js?key=AIzaSyBHJxzPHD_egYnhxntqcvfem35YRjruzAg&callback=initMap"

so I want to write this to src but I couldn't do it.

<script async defer

            src=localStorage.getItem("MapCode")>
    </script>

How can I solve this?Thanks in advance

Share Improve this question edited Jun 26, 2018 at 8:50 xomena 32.2k6 gold badges96 silver badges125 bronze badges asked Jun 1, 2016 at 6:26 Cagri TacyildizCagri Tacyildiz 17.6k6 gold badges37 silver badges61 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

You can load Google Maps JavaScript API dynamically. For this purpose you can create the following code:

function loadScript() {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = localStorage.getItem("MapCode");
    document.body.appendChild(script);
}

window.onload = loadScript;

Please look at the sample code http://jsbin./carobun/edit?html,output

code snippet:

var map;

function initialize() {
    var c = new google.maps.LatLng(54.8867537,-1.557352);
    var mapOptions = {
        zoom:7,
        center: c
    };
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}
 
function loadScript() {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'https://maps.googleapis./maps/api/js?v=3' +
        '&key=AIzaSyDztlrk_3CnzGHo7CFvLFqE_2bUKEq1JEU&callback=initialize';
    document.body.appendChild(script);
}

window.onload = loadScript;    
html,
body,
#map-canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<div id="map-canvas"></div>

发布评论

评论列表(0)

  1. 暂无评论