Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var geocoder;
- var map;
- var marker;
- var directionsService = new google.maps.DirectionsService();
- function initialize() {
- var latlng = new google.maps.LatLng(-18.898123, -48.265920);
- var options = {
- zoom: 15,
- center: latlng,
- mapTypeId: google.maps.MapTypeId.ROADMAP
- };
- map = new google.maps.Map(document.getElementById("mapa"), options);
- geocoder = new google.maps.Geocoder();
- marker = new google.maps.Marker({
- map: map,
- draggable: true,
- });
- marker.setPosition(latlng);
- }
- $(document).ready(function () {
- initialize();
- function carregarNoMapa(endereco) {
- geocoder.geocode({ 'address': endereco + ', Brasil', 'region': 'BR' }, function (results, status) {
- if (status == google.maps.GeocoderStatus.OK) {
- if (results[0]) {
- var latitude = results[0].geometry.location.lat();
- var longitude = results[0].geometry.location.lng();
- $('#txtEndereco').val(results[0].formatted_address);
- $('#txtLatitude').val(latitude);
- $('#txtLongitude').val(longitude);
- var location = new google.maps.LatLng(latitude, longitude);
- marker.setPosition(location);
- map.setCenter(location);
- map.setZoom(16);
- }
- }
- })
- }
- google.maps.event.addListener(marker, 'drag', function () {
- geocoder.geocode({ 'latLng': marker.getPosition() }, function (results, status) {
- if (status == google.maps.GeocoderStatus.OK) {
- if (results[0]) {
- $('#txtEndereco').val(results[0].formatted_address);
- $('#txtLatitude').val(marker.getPosition().lat());
- $('#txtLongitude').val(marker.getPosition().lng());
- }
- }
- });
- });
- /*$("#txtEndereco").autocomplete({
- source: function (request, response) {
- geocoder.geocode({ 'address': request.term + ', Brasil', 'region': 'BR' }, function (results, status) {
- response($.map(results, function (item) {
- return {
- label: item.formatted_address,
- value: item.formatted_address,
- latitude: item.geometry.location.lat(),
- longitude: item.geometry.location.lng()
- }
- }));
- })
- },
- select: function (event, ui) {
- $("#txtLatitude").val(ui.item.latitude);
- $("#txtLongitude").val(ui.item.longitude);
- var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);
- marker.setPosition(location);
- map.setCenter(location);
- map.setZoom(16);
- }
- });*/
- $("#btnEndereco").click(function(){
- var directionsDisplay = new google.maps.DirectionsRenderer();
- var request = {
- origin: $("#txtEndereco").val(),
- destination: marker.position,
- travelMode: google.maps.DirectionsTravelMode.DRIVING
- };
- directionsService.route(request, function(response, status) {
- if (status == google.maps.DirectionsStatus.OK) {
- directionsDisplay.setDirections(response);
- directionsDisplay.setMap(map);
- }
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment