Google Map javascript api key

时间:2016-06-01 06:26:49

标签: javascript html google-maps cordova google-maps-api-3

我正在使用cordova应用程序,并有一个浏览器键用于地图。

localStorage.getItem("MapCode")

但是我想从服务器上取下我的密钥,然后我把这个密钥存储为

<script async defer

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

这个locastorage给出了

  

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

所以我想把它写给src,但我不能这样做。

[TestFixture]
public class Tester
{

    [Test]
    public async Task TestSendTestMail()
    {
        // Arrange

        // Act
        await EbayProxy.Instance.SendTestMail();

        // Assert
    }
}

public async Task SendTestMail()
{
    MailMessage mail = new MailMessage();
    mail.From = new MailAddress(_mailFrom);
    mail.To.Add(_mailTo);

    mail.Subject = "Test Mail Subject Async";

    mail.Body = "Test Mail Body";

    mail.IsBodyHtml = true;

    SmtpClient smtp = new SmtpClient(_smtpClient, Convert.ToInt32(_smtpPort));
    smtp.EnableSsl = true;
    smtp.Credentials = new NetworkCredential(_mailFrom, _mailFromPassword);

    await smtp.SendMailAsync(mail); // Not sending mail :(
}

我该如何解决这个问题?提前致谢

1 个答案:

答案 0 :(得分:2)

您可以动态加载Google Maps JavaScript API。为此,您可以创建以下代码:

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

window.onload = loadScript;

请查看示例代码http://jsbin.com/carobun/edit?html,output

代码段

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.com/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>