如何将这个jQuery添加到我的Javascript中?

时间:2014-11-18 08:50:23

标签: javascript jquery

我对JS和jQuery都很陌生,我有这个代码:

<html>
<style>
/**
* Default attributes for gadget body.
*/
body {
 font-family: Arial;
background: none transparent;
padding: 0px;
}
html, body, #map_canvas {
margin: 0;
padding: 0;
height: 100%;
}
</style>
<body>
<div id="map_canvas"></div> 
<script>
var APIKey = "MyKeyValueIsInHere";   
var geocoder;
var map;
var marker;
var locationTest = 'http://nominatim.openstreetmap.org/reverse?format=json&lat=55.653363&lon=12.547604&zoom=18&addressdetails=1';
var lat = 55.653363;
var lng = 12.547604;

function initialize() {
var latlng = new google.maps.LatLng(lat,lng);
// set the options for zoom level and map type
var myOptions = {
    zoom: 15,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
}
// create a map in the map_canvas div element
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
map.setCenter(latlng);
        var marker = new google.maps.Marker({
            map: map, 
            position: latlng

        });
        alert(locationTest);
        var text = JSON.parse(locationTest);
        var infoWindow = new google.maps.InfoWindow();

        var houseNumber = text.address.house_number;
        var road = text.address.road;
        var suburb = text.address.suburb;
        var zipCode = text.address.postcode;
        var city = text.address.city;

        var address = road + " " + houseNumber + ", " + zipCode + " " + suburb + " i " + city;


        google.maps.event.addListener(marker, 'click', function() {
        infoWindow.setContent(address);
        infoWindow.open(map, this);
        });
}
function loadScript() {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://maps.googleapis.com/maps/api/js?key=" + APIKey + "&sensor=false&callback=initialize";  

document.body.appendChild(script);
}   

loadScript();   
</script>
</body>
</html>

我需要加入以下代码:

$('#c').load('http://nominatim.openstreetmap.org/reverse?format=json&lat=55.653363&lon=12.547604&zoom=18&addressdetails=1', function (response) {
console.log('response is', response);
var b = $('#c').html();
console.log('b is', b);
var a = JSON.parse(b);
console.log('a is ', a);
$('#d').html(a.address.neighbourhood);
});

基本上,此代码应显示在谷歌地图上的位置,该位置由长和纬度设置。在标记上,我想要一个信息框弹出地址。我发现的jQuery代码似乎工作here,但我现在已经不足以将其包含在我的JS中。我希望有人可以帮助我,我无法弄清楚如何实现它。

1 个答案:

答案 0 :(得分:1)

您需要在标记中添加jquery脚本链接。

<head>
    <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
</head>