如何在地图API中阅读国家城市纬度经度

时间:2017-01-08 12:40:29

标签: javascript

<script>

    function initMap() {
        var map = new google.maps.Map(document.getElementById('map'), {
            center: { lat: 33.738045, lng: 73.084488 },
            zoom: 10
        });
        new AutocompleteDirectionsHandler(map);
    }



    /**
    * @constructor
   */
    function AutocompleteDirectionsHandler(map) {
        this.map = map;
        this.originPlaceId = null;
        this.destinationPlaceId = null;
        this.travelMode = 'DRIVING';
        var originInput = document.getElementById('origin-input');
        var destinationInput = document.getElementById('destination-input');
        var modeSelector = document.getElementById('mode-selector');
        this.directionsService = new google.maps.DirectionsService;
        this.directionsDisplay = new google.maps.DirectionsRenderer({ 'draggable': true });
        this.directionsDisplay.setMap(map);

        var originAutocomplete = new google.maps.places.Autocomplete(
            originInput, { placeIdOnly: true });
        var destinationAutocomplete = new google.maps.places.Autocomplete(
            destinationInput, { placeIdOnly: true });

        this.setupClickListener('changemode-walking', 'DRIVING');


        this.setupPlaceChangedListener(originAutocomplete, 'ORIG');
        this.setupPlaceChangedListener(destinationAutocomplete, 'DEST');

        this.map.controls[google.maps.ControlPosition.TOP_LEFT].push(originInput);
        this.map.controls[google.maps.ControlPosition.TOP_LEFT].push(destinationInput);
        this.map.controls[google.maps.ControlPosition.TOP_LEFT].push(modeSelector);
    }

    // Sets a listener on a radio button to change the filter type on Places
    // Autocomplete.
    AutocompleteDirectionsHandler.prototype.setupClickListener = function (id, mode) {
        var radioButton = document.getElementById(id);
        var me = this;
        radioButton.addEventListener('click', function () {
            me.travelMode = 'DRIVING';
            me.route();
        });
    };

    AutocompleteDirectionsHandler.prototype.setupPlaceChangedListener = function (autocomplete, mode) {
        var me = this;
        autocomplete.bindTo('bounds', this.map);
        autocomplete.addListener('place_changed', function () {
            var place = autocomplete.getPlace();
            if (!place.place_id) {
                window.alert("Please select an option from the dropdown list.");
                return;
            }
            if (mode === 'ORIG') {
                me.originPlaceId = place.place_id;
            } else {
                me.destinationPlaceId = place.place_id;
            }
            me.route();
        });

    };

    AutocompleteDirectionsHandler.prototype.route = function () {
        if (!this.originPlaceId || !this.destinationPlaceId) {
            return;
        }
        var me = this;

        this.directionsService.route({
            origin: { 'placeId': this.originPlaceId },
            destination: { 'placeId': this.destinationPlaceId },
            travelMode: this.travelMode
        }, function (response, status) {
            if (status === 'OK') {
                me.directionsDisplay.setDirections(response);
            } else {
                window.alert('Directions request failed due to ' + status);
            }
        });
    };


</script>

目前我正在使用asp.net C#web表单创建一个项目,该表单读取位置并将其保存在数据库中

我想在拖动标记时找到国家/地区城市纬度经度,并希望根据标记位置绑定搜索框

1 个答案:

答案 0 :(得分:0)

如果您在追踪初始出发地和目的地的详细信息(国家/地区,城市,纬度,经度)之后,请在拖动路线之前,从{ placeIdOnly: true }来电中删除google.maps.places.Autocomplete()

如果您追踪拖动标记的地方的详细信息(国家,城市,纬度,经度),当用户拖动路线时,您需要

  1. directionsDisplay上为'directions_changed'添加一位听众。请参阅directions-draggable example
  2. 在回调函数中,检查所有start_location的{​​{1}}和最后一个legs,以获取航路点&#39; latlngs。