将纬度和经度传递给我的地图脚本 [英] passing the latitude and longitude to my map script

查看:109
本文介绍了将纬度和经度传递给我的地图脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用纬度和经度在google地图上显示用户地址。我得到了拉特&通过使用java脚本,并且还具有地图脚本。但我不知道如何将它们结合起来,我只需要将lat和long传递给show map脚本function initialize()。这是我的代码:

I show the user address in the google map by using the latitude and longitude. I got the lat & long by using an java-script, and also have the map script. but I dont know how to combine them, Simply what I need is pass the lat and long into the show map script "function initialize()". this is my code:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var map;
var geocoder;
var centerChangedLast;
var reverseGeocodedLast;
var currentReverseGeocodeResponse;

var geocoder = new google.maps.Geocoder();
var address = "<?php echo $address;?>";

geocoder.geocode( { 'address': address}, function(results, status) {

  if (status == google.maps.GeocoderStatus.OK) {
    var latitude = results[0].geometry.location.lat();
    var longitude = results[0].geometry.location.lng();
    //alert(longitude);
  } 
});     

function initialize() {

    var latlng = new google.maps.LatLng(latitude,longitude);
    var myOptions = {
        zoom: 10,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    geocoder = new google.maps.Geocoder();

    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        title: "My Location"
    });

}

</script>


推荐答案

在初始化函数中对地址进行地理编码。使用返回的经度和纬度初始化地图。

Geocode the address in your initialize function. Use the returned latitude and longitude to initialize the map.

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var map;
var geocoder;
var centerChangedLast;
var reverseGeocodedLast;
var currentReverseGeocodeResponse;

var geocoder = new google.maps.Geocoder();
var address = "<?php echo $address;?>";


function initialize() {
  geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      var latitude = results[0].geometry.location.lat();
      var longitude = results[0].geometry.location.lng();
      var latlng = new google.maps.LatLng(latitude,longitude);
      var myOptions = {
        zoom: 10,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
      geocoder = new google.maps.Geocoder();

      var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        title: "My Location"
      });

    } 
  });
}

</script>

工作示例

这篇关于将纬度和经度传递给我的地图脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆