dongru3726 2013-11-19 10:01
浏览 69

如何使用Google Map查找当前位置(经度和纬度)?

I just want to ask how can I get the current latitude and longitude using Google Map? Because I am creating a Map Tool for adjusting maps in database. If there is a given latitude and longitude simply display the map. I have no problem with that. But if the longitude and latitude is NULL or 0. How can I get the exact latitude and longitude?

I have this code:

for ($i = 0; $i <= $companies_count; $i++) :

$company = $companies[$i];
$company_id = $company['company_id'];

//file information from textfile
$file_id = $addresses_file[$i]['company_id'];
$file_latitude = $addresses_file[$i]['new_lat'];
$file_longitude = $addresses_file[$i]['new_long'];

foreach($addresses_file as $y){

        if($company_id == $y['company_id']){

            $lat = $y['new_lat'];
            $long = $y['new_long'];

        }else{

            $lat = $additionals[$company_id]['geo_lat'];
                $long = $additionals[$company_id]['geo_long'];

        if($lat == 0 || $lat == NULL){
             //set lat to current position
        }

        if($long == 0 || $long == NULL){
             //set long to current position
        }



        }

}

endfor;

In my javascript:

<script>    

            var maps = {},
            geocoder = null;

            function showAddress(address, i) {

                if (geocoder) {

                    geocoder.geocode( { 'address' : address },   

                        function(results, status) {  

                            if (status == google.maps.GeocoderStatus.OK) {   

                                //var id = $("[name='selected']").val();
                                var id = i;
                                var center = results[0].geometry.location;

                                document.getElementById("lat_" + id).innerHTML = center.lat().toFixed(5);
                                document.getElementById("long_" + id).innerHTML = center.lng().toFixed(5);

                                maps[id].mapObj.setCenter(center);
                                maps[id].marker.setPosition(center);

                                $('html, body').animate({
                                    scrollTop: $("tr[rel='" + id + "']").offset().top
                                }, 1000);

                            } else {
                                alert("Geocode was not successful for the following reason: " + status);
                            }
                        }

                    );
                }

            }

            function fn_initialize() {

                geocoder = new google.maps.Geocoder();

                var hh_map = {
                        init: function(lat, lon, z_lvl, label, id){

                            var latlng = new google.maps.LatLng(lat, lon);

                            var mapOptions = {
                                zoom: z_lvl,
                                mapTypeId: google.maps.MapTypeId.ROADMAP,
                                streetViewControl: false, 
                                overviewMapControl: false, 
                                mapTypeControl: false, 
                                panControl: false, 
                                zoomControlOptions: { 
                                  style: google.maps.ZoomControlStyle.SMALL, 
                                  position: google.maps.ControlPosition.LEFT_BOTTOM
                                },
                                center: latlng,
                            };                                      


                            var map = new google.maps.Map(document.getElementById("map_canvas_" + id), mapOptions);

                            var marker = new google.maps.Marker({
                                map: map, 
                                position: map.getCenter(),
                                title: label,
                                draggable: true
                            });                                        

                            // this will update update the lat_{id} and long_{id}
                            google.maps.event.addListener(marker, 'dragend', function(e) {

                                var center = marker.getPosition();

                                document.getElementById("lat_" + id).innerHTML = center.lat().toFixed(5);
                                document.getElementById("long_" + id).innerHTML = center.lng().toFixed(5);
                            });

                            // this will center the marker in the map and update the lat_{id} and long_{id}
                            /*google.maps.event.addListener(map, 'idle', function(e) {
                                var center = map.getCenter();

                                document.getElementById("lat_" + id).innerHTML = center.lat().toFixed(5);
                                document.getElementById("long_" + id).innerHTML = center.lng().toFixed(5);

                                marker.setPosition(center);                            
                            });*/

                            maps[id] = {'mapObj' : map, 'marker' : marker};
                        }
                    };

                $('.map_canvas').each(function(){
                    if ($(this).data('lat') && $(this).data('long')) {
                        hh_map.init($(this).data('lat'), $(this).data('long'), 16, $(this).data('label'), $(this).data('company_id'));
                    }
                })
            }

            function loadScript() {
                var script = document.createElement("script");
                script.type = "text/javascript";
                script.src = "http://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&callback=fn_initialize";
                document.body.appendChild(script);
            }

            window.onload = loadScript;

            $(document).ready(function(){

                //update address
                $('.save-button').each(function(index, elm){

                    $(elm).click(function(){

                        var id = $(this).attr("id"),
                        val_lat = $('#lat_' + id).html(),
                        val_long = $('#long_' + id).html();

                        if (val_lat.length > 0 && val_long.length > 0) {

                            var x = confirm("Are you sure you want to update this?");

                            if(x == true){

                                $('<form action="" method="POST">' +
                                '<input type="hidden" name="company_id" value="' + id + '">' +
                                '<input type="hidden" name="new_lat" value="' + val_lat + '">' +
                                '<input type="hidden" name="new_long" value="' + val_long + '">' +
                                '</form>').submit();        

                            }else{
                                return false;
                            }

                        } else {
                            alert('New locations are empty!');    
                        }

                        return false;
                    })

                })

                //revert address
                $('.revert-button').each(function(index, elm){

                    $(elm).click(function(){

                        var id = $(this).attr("id"),
                        val_lat = $('#lat_' + id).html(),
                        val_long = $('#long_' + id).html();

                        if (val_lat.length > 0 && val_long.length > 0) {

                            var x = confirm("Are you sure you want to revert this?");

                            if(x == true){

                                $('<form action="" method="POST">' +
                                '<input type="hidden" name="company_id" value="' + id + '">' +
                                '<input type="hidden" name="new_lat" value="' + val_lat + '">' +
                                '<input type="hidden" name="new_long" value="' + val_long + '">' +
                                '</form>').submit();        

                            }else{
                                return false;
                            }

                        } else {
                            alert('New locations are empty!');    
                        }

                        return false;
                    })

                })

            })

        </script>
  • 写回答

1条回答 默认 最新

  • dongqiang2358 2013-11-19 10:06
    关注

    With JavaScript, you can use:

    var center = map.getCenter();
    alert(center.lat() + ', ' + center.lng());
    

    The top answer shown in the linked duplicate question is for Google Maps V2.

    评论

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?