How to get google place api from autocomplete.getPlace ()

I would like to pass the longitude / latitude results from google api places to my MVC application route values. I am not sure how to get it to my route values ​​or how to return javascript to html values. Right now

var result = autocomplete.getPlace();

returns undefined. Thus, it does not even look like work, although autocomplete for places works fine.

var input = document.getElementById('location');
var options = {
types: ['(cities)'],
componentRestrictions: { country: "us" }
};
var autocomplete = new google.maps.places.Autocomplete(input, options);
var t = 2;
var result = autocomplete.getPlace(); //result is undefined
var i = 1;
//var lat = result.geometry.location.lat;
//var lon = result.geometry.location.lon;

Here is my form that has an input field and a script tag where the location is. I am trying to get lon / lat values ​​for route values ​​of 5.6

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js? v=3.exp&amp;sensor=false&amp;libraries=places"></script>

@{
    var routeValues = new RouteValueDictionary();
    routeValues.Add("Longitude", "5");
    routeValues.Add("Latitude", "6");
}

@using (Html.BeginForm("Results", "Home", routeValues, FormMethod.Post))
{
    <p>
        <input type="text" name="location" id="location" placeholder="Search For A Studio" />
    </p>
    <p>
        <input class="btn btn-primary btn-lg" value='Submit' type="submit"/>
    </p>
}

Here is my action in the controller

public ActionResult Results(string longitude, string latitude)
    {
        var repo = new YogaSpaceRepository();

            /// 1000 Ocean Ave
            DbGeography myLocation = DbGeography.FromText("POINT(-122.453164 37.723057)");
            IQueryable<YogaSpace> spaces = repo.AllWithinDistance(myLocation);


        return View(spaces);
    }

* UPDATE - autocomplete.getPlace() . javascript onclick . , ! (/). , lat/lon, , , , , , , .

var input = document.getElementById('location');
var options = {
   types: ['(cities)'],
   componentRestrictions: { country: "us" }
};
var autocomplete = new google.maps.places.Autocomplete(input, options);

function getGeometry() {
   var place = autocomplete.getPlace();

   var lat = place.geometry.location.lat;
   var lon = place.geometry.location.lon;
}
+4
2

lat lang geometry.location

place.geometry.location.lat()
place.geometry.location.lng()
+5

, lat lng LatLng, , lat/lng:

var place = autocomplete.getPlace();
var lat = place.geometry.location.lat();
var lng = place.geometry.location.lat();

function initMap() {
    var map = new google.maps.Map(document.getElementById('map'), {
        center: { lat: -33.8688, lng: 151.2195 },
        zoom: 13
    });
    var input = /** @type {!HTMLInputElement} */(
        document.getElementById('pac-input'));

    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
    

    var autocomplete = new google.maps.places.Autocomplete(input);
    autocomplete.bindTo('bounds', map);

    var infowindow = new google.maps.InfoWindow();
    var marker = new google.maps.Marker({
        map: map,
        anchorPoint: new google.maps.Point(0, -29)
    });


    autocomplete.addListener('place_changed', function () {
        infowindow.close();
        marker.setVisible(false);
        var place = autocomplete.getPlace();
        if (!place.geometry) {
            window.alert("Autocomplete returned place contains no geometry");
            return;
        }

        //displayResult(place);

        // 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.
        }
        marker.setIcon(/** @type {google.maps.Icon} */({
            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.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(' ');
        }

        //infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
        infowindow.setContent('<div><strong>' + "Location(" + place.geometry.location.lat() + "," + place.geometry.location.lng() + ")" + '</strong><br>');
        infowindow.open(map, marker);
    });

}
        html, body {
            height: 100%;
            margin: 0;
            padding: 0;
        }

        #map {
            height: 100%;
        }

        .controls {
            margin-top: 10px;
            border: 1px solid transparent;
            border-radius: 2px 0 0 2px;
            box-sizing: border-box;
            -moz-box-sizing: border-box;
            height: 32px;
            outline: none;
            box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
        }

        #pac-input {
            background-color: #fff;
            font-family: Roboto;
            font-size: 15px;
            font-weight: 300;
            margin-left: 12px;
            padding: 0 11px 0 13px;
            text-overflow: ellipsis;
            width: 300px;
        }

            #pac-input:focus {
                border-color: #4d90fe;
            }

        .pac-container {
            font-family: Roboto;
        }
 <input id="pac-input" class="controls" type="text"
           placeholder="Enter a location">
    <div id="map"></div>
    <script src="https://maps.googleapis.com/maps/api/js?signed_in=true&libraries=places&callback=initMap"
            async defer></script>
Hide result
+3

All Articles