谷歌地图有城市名称的经纬度? [英] Google maps get latitude and longitude having city name?

查看:121
本文介绍了谷歌地图有城市名称的经纬度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过提供城市名称来获取城市的经纬度。它应该适用于大多数城市,无论用户输入城市的方式如何。



例如:

城市可以'miami,US'或city可以'miami,united states'

我如何打印它的纬度?

解决方案

你可以在这里找到jsfiddled的代码: http://jsfiddle.net/YphZw/
或以下:

$(#btn)。click(function(){var geocoder = new google.maps .Geocoder(); geocoder.geocode({'address':'miami,us'},function(results,status){if(status == google.maps.GeocoderStatus.OK){alert(location:+ results [0] .geometry.location.lat()++ results [0] .geometry.location.lng());} else {alert(Something got wrong+ status);}}) ;});

< script src =https: //ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js\"></script><html><head> < script type =text / javascriptsrc =https://maps.googleapis.com/maps/api/js?sensor=false>< / script>< / head>< body> < / body>< / html>< / code>< / pre><如果您想要更多Javascript API的示例,请尝试以下链接:

maps / documentation / javascript / examples /> https://developers.google.com/maps/documentation/javascript/examples/



代码I编写灵感来自于 geocoding-simple 示例。



问候。

编辑1:

您可以使用非官方的PHP库来实现它。检查此示例:
http://www.bradwedell.com/phpgooglemapapi/demos/geocoding.php
(代码位于底部=

I want to get latitude and longitude of a city by providing the API with the city name. It should work for most cities regardless how the user inputs the city.

For example:

City can be 'miami, US' or city can be 'miami, united states'

How do I print its latitude?

解决方案

You can find the code jsfiddled here : http://jsfiddle.net/YphZw/ or below :

$("#btn").click(function(){
            var geocoder =  new google.maps.Geocoder();
    geocoder.geocode( { 'address': 'miami, us'}, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            alert("location : " + results[0].geometry.location.lat() + " " +results[0].geometry.location.lng()); 
          } else {
            alert("Something got wrong " + status);
          }
        });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<head>
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
</head>
<body>
    <input id="btn" type="button" value="search for miami coordinates" />
</body>
</html>

If you want more examples for the Javascript API, try this link : https://developers.google.com/maps/documentation/javascript/examples/

The code I wrote is inspired from the geocoding-simple sample.

Regards.

EDIT 1:

You can achieve it using an non-official PHP library. Check this example : http://www.bradwedell.com/phpgooglemapapi/demos/geocoding.php (The code is at the bottom=

这篇关于谷歌地图有城市名称的经纬度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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