如何定义纬度和谷歌地图中的经度而不是地址?

时间:2015-09-08 08:35:06

标签: javascript google-maps

我在我的应用程序中显示Map。我已经添加了下面的代码,现在地图正在显示特定的城市名称。我的问题是我需要通过使用特定城市的纬度和经度来获取位置。

我不想要任何新代码。是否可以通过@ item.Latitude @ item.Longitude而不是@ item.city来获取此代码中的纬度和经度值

<div class="gMapsCanvas" data-address="@item.city"></div>

    <script type="text/javascript">
            var GoogleMap = function ga(canvas, address) {
                // debugger;
                var _parent = this;

                //this.location = new google.maps.LatLng(-34.397, 150.644);

                var options =
                {
                    center: this.location,
                    zoom: 11,
                    mapTypeId: google.maps.MapTypeId.ROADMAP,
                    mapTypeControlOptions:
                    {
                        style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
                        position: google.maps.ControlPosition.TOP_CENTER
                    },
                    streetViewControl: false
                };

                this.map = new google.maps.Map(canvas, options);

                var geocoder = new google.maps.Geocoder();

                geocoder.geocode({ 'address': address }, function (results, status) {
                    if (status != google.maps.GeocoderStatus.OK)
                        return;

                    _parent.location = results[0].geometry.location;

                    var marker = new google.maps.Marker(
                    {
                        map: _parent.map,
                        position: _parent.location
                    });

                    _parent.resize();
                });
            };

            GoogleMap.prototype.resize = function () {
                google.maps.event.trigger(this.map, "resize");

                this.map.setCenter(this.location);
            }

            var Maps = function (classes) {
                var _parent = this;

                this.maps = new Array();

                classes.each(function () {
                    _parent.maps.push(new GoogleMap($(this).get(0), $(this).attr("data-address")));
                });
            };

            Maps.prototype.resize = function () {
                for (var cnt = 0; cnt < this.maps.length; cnt++) {
                    this.maps[cnt].resize();
                }
            };

            var maps;

        </script>
    <script type="text/javascript">
        $(".tiptext").mouseover(function () {
            $(this).children(".description").show();

            maps = new Maps($(".gMapsCanvas"));

        }).mouseout(function () {
            $(this).children(".description").hide();

        });

    </script>

2 个答案:

答案 0 :(得分:1)

您可以通过多种方式在浏览器中显示Google地图。 但最简单和最好的两种方法是:  1.使用Goggle MAP API https://developers.google.com/maps/?hl=en  2. Google Polymer如下所示:

     <!-- Polyfill Web Components support for older browsers -->
<script src="components/webcomponentsjs/webcomponents-lite.min.js"></script>

<!-- Import element -->
<link rel="import" href="components/google-map/google-map.html">

<!-- Use element -->
<google-map latitude="37.790" longitude="-122.390"></google-map>

答案 1 :(得分:0)

尝试{&#39; latlng&#39 ;:纬度+&#39;,&#39; +经度}而不是{&#39;地址&#39;:地址}。

我没有测试过,但似乎根据googlemap webservice api工作:https://developers.google.com/maps/documentation/geocoding/intro#ReverseGeocoding