我想知道是否有人可以帮助我。
我已经整理了这个页面,它允许用户通过地图点击或手动输入地址来对地址进行地理编码,这可以按预期工作。此代码是对此处找到的教程的轻微改编。
除了上述功能之外,我现在要做的是添加代码,该代码放置一个带有已经保存在 MySQL 数据库中的坐标的标记。
这是我为尝试实现这一目标而编写的代码:
<script type="text/javascript">
var icon = new google.maps.MarkerImage("images/location-marker-2.png")
new google.maps.Point(16, 32);
var center = null;
var map = null;
var bounds = new google.maps.LatLngBounds();
function addMarker(lat, lng, info) {
var pt = new google.maps.LatLng(lat, lng);
bounds.extend(pt);
var marker = new google.maps.Marker({
position: pt,
icon: icon,
map: map
});
}
function initMap() {
map = new google.maps.Map(document.getElementById("gmaps-canvas"), {
center: new google.maps.LatLng(0, 0),
zoom: 6,
scrollwheel: true,
draggable: true,
mapTypeId: google.maps.MapTypeId.SATELLITE
});
<?php
include("admin/link.php");
include("admin/opendb.php");
$query = mysql_query("SELECT * FROM `detectinglocations` WHERE `locationid` = '$lid'");
while ($row = mysql_fetch_array($query)){
$locationname=$row['locationname'];
$osgb36lat=$row['osgb36lat'];
$osgb36lon=$row['osgb36lon'];
echo ("addMarker($osgb36lat, $osgb36lon,'<b>$locationname</b><br/>');\n");
}
mysql_close($connect);
?>
center = bounds.getCenter();
map.fitBounds(bounds);
}
</script>
我遇到的问题是我可以使geocode
功能正常工作,但是当我尝试添加标记时,没有任何反应,也没有收到任何错误Chrome
.
作为 POST 的补充,我在下面添加了 Geocode JS 文件:
var geocoder;
var map;
var marker;
// initialise the google maps objects, and add listeners
function gmaps_init(){
// center of the universe
var latlng = new google.maps.LatLng(54.312195845815246, -4.45948481875007);
var options = {
zoom: 6,
center: latlng,
scrollwheel: true,
draggable: true,
mapTypeId: google.maps.MapTypeId.SATELLITE
};
// create our map object
map = new google.maps.Map(document.getElementById("gmaps-canvas"), options);
// the geocoder object allows us to do latlng lookup based on address
geocoder = new google.maps.Geocoder();
// the marker shows us the position of the latest address
marker = new google.maps.Marker({
map: map,
draggable: true
});
// event triggered when marker is dragged and dropped
google.maps.event.addListener(marker, 'dragend', function() {
geocode_lookup( 'latLng', marker.getPosition() );
var point = marker.getPosition();
map.panTo(point);
document.getElementById('osgb36lat').value = marker.position.lat();
document.getElementById('osgb36lon').value = marker.position.lng();
});
// event triggered when map is clicked
google.maps.event.addListener(map, 'click', function(event) {
marker.setPosition(event.latLng)
geocode_lookup( 'latLng', event.latLng );
document.getElementById('osgb36lat').value = marker.position.lat();
document.getElementById('osgb36lon').value = marker.position.lng();
});
}
// move the marker to a new position, and center the map on it
function update_map( geometry ) {
map.fitBounds( geometry.viewport )
marker.setPosition( geometry.location )
document.getElementById('osgb36lat').value = marker.position.lat();
document.getElementById('osgb36lon').value = marker.position.lng();
}
// fill in the UI elements with new position data
function update_ui( address, latLng ) {
$('#gmaps-input-address').autocomplete("close");
$('#gmaps-input-address').val(address);
$('#gmaps-output-latitude').html(latLng.lat());
$('#gmaps-output-longitude').html(latLng.lng());
}
// Query the Google geocode object
//
// type: 'address' for search by address
// 'latLng' for search by latLng (reverse lookup)
//
// value: search query
//
// update: should we update the map (center map and position marker)?
function geocode_lookup( type, value, update ) {
// default value: update = false
update = typeof update !== 'undefined' ? update : false;
request = {};
request[type] = value;
geocoder.geocode(request, function(results, status) {
$('#gmaps-error').html('');
if (status == google.maps.GeocoderStatus.OK) {
// Google geocoding has succeeded!
if (results[0]) {
// Always update the UI elements with new location data
update_ui( results[0].formatted_address,
results[0].geometry.location )
document.getElementById('osgb36lat').value = marker.position.lat();
document.getElementById('osgb36lon').value = marker.position.lng();
// Only update the map (position marker and center map) if requested
if( update ) { update_map( results[0].geometry ) }
} else {
// Geocoder status ok but no results!?
$('#gmaps-error').html("Sorry, something went wrong. Try again!");
}
} else {
// Google Geocoding has failed. Two common reasons:
// * Address not recognised (e.g. search for 'zxxzcxczxcx')
// * Location doesn't map to address (e.g. click in middle of Atlantic)
if( type == 'address' ) {
// User has typed in an address which we can't geocode to a location
$('#gmaps-error').html("Sorry! We couldn't find " + value + ". Try a different search term, or click the map." );
} else {
// User has clicked or dragged marker to somewhere that Google can't do a reverse lookup for
// In this case we display a warning, clear the address box, but fill in LatLng
$('#gmaps-error').html("Woah... that's pretty remote! You're going to have to manually enter a place name." );
update_ui('', value)
}
};
});
};
// initialise the jqueryUI autocomplete element
function autocomplete_init() {
$("#gmaps-input-address").autocomplete({
// source is the list of input options shown in the autocomplete dropdown.
// see documentation: http://jqueryui.com/demos/autocomplete/
source: function(request,response) {
// the geocode method takes an address or LatLng to search for
// and a callback function which should process the results into
// a format accepted by jqueryUI autocomplete
geocoder.geocode( {'address': request.term }, function(results, status) {
response($.map(results, function(item) {
return {
label: item.formatted_address, // appears in dropdown box
value: item.formatted_address, // inserted into input element when selected
geocode: item // all geocode data: used in select callback event
}
}));
})
},
// event triggered when drop-down option selected
select: function(event,ui){
update_ui( ui.item.value, ui.item.geocode.geometry.location )
update_map( ui.item.geocode.geometry )
}
});
// triggered when user presses a key in the address box
$("#gmaps-input-address").bind('keydown', function(event) {
if(event.keyCode == 13) {
geocode_lookup( 'address', $('#gmaps-input-address').val(), true );
// ensures dropdown disappears when enter is pressed
$('#gmaps-input-address').autocomplete("disable")
} else {
// re-enable if previously disabled above
$('#gmaps-input-address').autocomplete("enable")
}
});
}; // autocomplete_init
$(document).ready(function() {
if( $('#gmaps-canvas').length ) {
gmaps_init();
autocomplete_init();
};
});
我已经为此工作了一段时间,但我似乎无法弄清楚我哪里出错了。
我将非常感激,我想知道是否有人可以看看这个并让我知道我哪里出错了。
非常感谢和亲切的问候