如何使用Google地图将地址代码为经纬度 [英] How to geocode an address into lat/long with Google maps

查看:197
本文介绍了如何使用Google地图将地址代码为经纬度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在谷歌地图上绘制几家公司,并理解我需要对这些公司进行地理编码。

我还在代码下面显示了该图的多个标记一个地图。



我如何对几个公司地址进行地理编码(使用以下地址作为第一个示例)并将其合并到当前的代码中?



我真的需要别人的帮助,因为我无法理解Google文档以及将其与我已有的文档整合。

解决方案

您可以使用 Google地理编码获得你的邮政编码的坐标编辑:我不是很喜欢你改变这种问题的感觉,但确定。请尝试像这样(未测试):

  //创建一个包含坐标的数组
/ /纽约,旧金山和西雅图
var places = [];

//为每个城市添加一个LatLng对象
//places.push(new google.maps.LatLng(40.756,-73.986));
//places.push(new google.maps.LatLng(37.775,-122.419));
//places.push(new google.maps.LatLng(47.620,-122.347));
//places.push(new google.maps.LatLng(-22.933,-43.184));
var result;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open(GET,http://maps.googleapis.com/maps/api/geocode/json?address=your+code&sensor=false\",true);
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4){
result = eval('('+ xmlhttp.responseText +')');
if(result.status ==OK){
var location = new google.maps.LatLng(result.results [0] .geometry.location.lat,result.results [0]。 geometry.location.lng);
places.push(位置);

尽管可能出现小错误,但这应该可行。



EDIT2:我刚才发现更简单的解决方案

a>:


  //创建一个包含坐标
//的数组,用于纽约,旧金山,和西雅图
var places = [];

//为每个城市添加一个LatLng对象
//places.push(new google.maps.LatLng(40.756,-73.986));
//places.push(new google.maps.LatLng(37.775,-122.419));
//places.push(new google.maps.LatLng(47.620,-122.347));
//places.push(new google.maps.LatLng(-22.933,-43.184));
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address':your + code},function(results,status){
if(status == google.maps.GeocoderStatus.OK){
map .setCenter(results [0] .geometry.location);
// var marker = new google.maps.Marker({
// map:map,
// position:results [ 0] .geometry.location
//});
places.push(results [0] .geometry.location);
} else {
alert(Geocode was not成功的原因如下:+ status;
}
});


I want to be able to plot several companies on a google map and understand I need to geocode these.

I also have the code below that plot's multiple markers on a map.

How can I Geocode several company addresses (using the following address as the first example) and incorporate it into the current code I have?

I really need someone's help as I can't make sense of the Google documentation as well as incorporating it with what I already have.

解决方案

you could use google's geocoding to obtain coordinates of your post codes

EDIT: I don't really like you changing the sense of the question like that but ok. Please try sth like that (its not tested):

// Creating an array that will contain the coordinates 
// for New York, San Francisco, and Seattle
var places = [];

// Adding a LatLng object for each city
//places.push(new google.maps.LatLng(40.756, -73.986));
//places.push(new google.maps.LatLng(37.775, -122.419));
//places.push(new google.maps.LatLng(47.620, -122.347));
//places.push(new google.maps.LatLng(-22.933, -43.184));
var result;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "http://maps.googleapis.com/maps/api/geocode/json?address=your+code&sensor=false",true);
 xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4) {
       result = eval('(' + xmlhttp.responseText + ')');
if (result.status == "OK") {
var location = new google.maps.LatLng(result.results[0].geometry.location.lat, result.results[0].geometry.location.lng);
places.push(location);

this should probably work, despite possible minor errors.

EDIT2: I have just now found simpler solution:

// Creating an array that will contain the coordinates 
// for New York, San Francisco, and Seattle
var places = [];

// Adding a LatLng object for each city
//places.push(new google.maps.LatLng(40.756, -73.986));
//places.push(new google.maps.LatLng(37.775, -122.419));
//places.push(new google.maps.LatLng(47.620, -122.347));
//places.push(new google.maps.LatLng(-22.933, -43.184));
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': "your+code"}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    map.setCenter(results[0].geometry.location);
    //var marker = new google.maps.Marker({
        //map: map,
        //position: results[0].geometry.location
    //});
    places.push(results[0].geometry.location);
  } else {
    alert("Geocode was not successful for the following reason: " + status);
  }
});

这篇关于如何使用Google地图将地址代码为经纬度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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