我有一个使用Google地图位置库的应用程序。用户可以使用此库搜索起点和终点,然后单击其中一个自动填充结果,并使用路线服务在这两个点之间绘制路线。
我已经到了需要将这些值添加到MySql数据库中的地步。我已经构建了将数据库内容转换为新地图的页面(我使用了虚拟内容)。虚拟内容包括'startlat','startlng','endlat'和'endlng'。基本上我需要的是将Google地图位置转换为单独的纬度和经度值,以便我可以将其传递到我的数据库中。
我已经阅读了Google的地理编码API,但我不确定如果我已经使用了地方信息库,是否需要进行地理编码。以下是我使用Places API的方法。
jQuery(document).ready(function($) {
var rendererOptions = {
draggable: true
};
var directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);;
var directionsService = new google.maps.DirectionsService();
// Load options
var mapOptions = {
center: new google.maps.LatLng(-33.8688, 151.2195),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
// Try HTML5 geolocation
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
map.setCenter(pos);
}, function() {
handleNoGeolocation(true);
});
} else {
// Browser doesn't support Geolocation
handleNoGeolocation(false);
}
function handleNoGeolocation(errorFlag) {
if (errorFlag) {
var content = 'Error: The Geolocation service failed.';
} else {
var content = 'Error: Your browser doesn\'t support geolocation.';
}
var options = {
map: map,
position: new google.maps.LatLng(53.801158, -1.549158),
content: content
};
var infowindow = new google.maps.InfoWindow(options);
map.setCenter(options.position);
}
// Load map
var map = new google.maps.Map(document.getElementById('map_canvas'),mapOptions);
// Popup window
var infowindow = new google.maps.InfoWindow();
var marker = new google.maps.Marker({
map: map
});
window.startLocation = null;
window.endLoaction = null;
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('directionsPanel'));
google.maps.event.addListener(directionsDisplay, 'directions_changed', function() {
});
// Autocomplete start
var startInput = document.getElementById('start');
var autocompleteStart = new google.maps.places.Autocomplete(startInput);
autocompleteStart.bindTo('bounds', map);
google.maps.event.addListener(autocompleteStart, 'place_changed', function() {
infowindow.close();
marker.setVisible(false);
startInput.className = '';
var place = autocompleteStart.getPlace();
if (!place.geometry) {
// Inform the user that the place was not found and return.
startInput.className = 'notfound';
return;
}
// If the place has a geometry, then present it on a map.
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(17); // Why 17? Because it looks good.
}
var image = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(35, 35)
};
marker.setIcon(image);
marker.setPosition(place.geometry.location);
marker.setVisible(true);
var address = '';
if (place.address_components) {
address = [
(place.address_components[0] && place.address_components[0].short_name || ''),
(place.address_components[1] && place.address_components[1].short_name || ''),
(place.address_components[2] && place.address_components[2].short_name || '')
].join(' ');
}
window.startLocation = place;
calcRoute();
//infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
//infowindow.open(map, marker);
});
// Autocomplete end
var endInput = document.getElementById('end');
var autocompleteEnd = new google.maps.places.Autocomplete(endInput);
autocompleteEnd.bindTo('bounds', map);
google.maps.event.addListener(autocompleteEnd, 'place_changed', function() {
infowindow.close();
marker.setVisible(false);
endInput.className = '';
var place = autocompleteEnd.getPlace();
if (!place.geometry) {
// Inform the user that the place was not found and return.
endInput.className = 'notfound';
return;
}
// If the place has a geometry, then present it on a map.
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(17); // Why 17? Because it looks good.
}
var image = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(35, 35)
};
marker.setIcon(image);
marker.setPosition(place.geometry.location);
marker.setVisible(true);
var address = '';
if (place.address_components) {
address = [
(place.address_components[0] && place.address_components[0].short_name || ''),
(place.address_components[1] && place.address_components[1].short_name || ''),
(place.address_components[2] && place.address_components[2].short_name || '')
].join(' ');
}
window.endLocation = place;
calcRoute();
});
function calcRoute() {
var selectedMode = document.getElementById("mode").value;
if(window.startLocation && window.endLocation) {
var request = {
origin: window.startLocation.formatted_address,
destination: window.endLocation.formatted_address,
travelMode: google.maps.DirectionsTravelMode[selectedMode]
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
$(".distance-value").html(response.routes[0].legs[0].distance.text)
$(".duration-value").html(response.routes[0].legs[0].duration.text)
directionsDisplay.setDirections(response);
}
});
}
}
//End Maps Stuff
}); //End jQuery Document Ready
更新 - 好的,所以我发现我不需要Geocode,而是我可以使用它......
var startlocation = place.geometry.location;
var startlat = location.lat();
var startlng = location.lng();
var endlocation = place.geometry.location;
var endlat = location.lat();
var endlng = location.lng();
但是如何在表单提交时将这些值传递到数据库中?