如何从C#中的地址获取纬度和经度
Posted
技术标签:
【中文标题】如何从C#中的地址获取纬度和经度【英文标题】:how to get latitude and longitude from address in C# 【发布时间】:2011-09-28 10:49:12 【问题描述】:我想根据地址获取经纬度。我已经对其进行了设置,因此它可以捕获将其显示为曼彻斯特的用户位置。
我想写一个 if 语句…………;
if there is an address
// set lat/long from address
这里是完整的编码;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GeoCoding.Google;
using GeoCoding;
using System.Web.Configuration;
namespace MVCFacebookTestApp.Models
public class Home
//public string UserID get; set;
public string FBID get; set;
public string AccessToken get; set;
public string FName get; set;
public string LName get; set;
public string Gender get; set;
public DateTime? DOB get; set;
public string Email get; set;
public DateTime DateLiked get; set;
public string ImageURL get; set;
//public string TestString get; set;
//public string TestString2 get; set;
public bool IsLiked get; set;
//public string ImgUrl get; set;
private Home()
//Console.WriteLine(GoogleAPIKey);
public static Home NotLiked()
Home h = new Home();
h.IsLiked = false;
return h;
public static Home Liked()
Home h = new Home();
h.IsLiked = true;
return h;
public void AddUser(string facebookID, string accessToken, string fName, string lName, DateTime dob, string email, DateTime dateLiked, string gender, string imageURL, string locationID, string locationValue)
Entities context = new Entities();
//DBNameDataContext myDB = new DBNameDataContext();
IEnumerable<User> users = context.Users.Where(u => u.FacebookID == facebookID);
if (users.Count() == 0)
//UserID = userID;
FBID = facebookID;
AccessToken = accessToken;
FName = fName;
LName = lName;
DOB = dob;
Email = email;
DateLiked = dateLiked;
Gender = gender;
ImageURL = imageURL;
User newUser = new User();
//newUser.UserID = 1;
newUser.FacebookID = facebookID;
newUser.AccessToken = accessToken;
newUser.FName = fName;
newUser.LName = lName;
newUser.Gender = gender;
newUser.DOB = DOB;
newUser.Email = email;
newUser.DateLiked = dateLiked;
newUser.ImageURL = imageURL;
context.Users.AddObject(newUser);
context.SaveChanges();
else if (users.Count() == 1)
User user0 = users.First();
if (user0.Gender != gender)
user0.Gender = gender;
if (user0.FName != fName)
user0.FName = fName;
if (user0.LName != lName)
user0.LName = lName;
if (user0.DOB != dob)
user0.DOB = dob;
if (user0.Email != email)
user0.Email = email;
if (user0.DateLiked != dateLiked)
user0.DateLiked = dateLiked;
if (user0.ImageURL != imageURL)
user0.ImageURL = imageURL;
context.SaveChanges();
FBID = user0.FacebookID;
AccessToken = user0.AccessToken;
FName = user0.FName;
LName = user0.LName;
DOB = user0.DOB;
Email = user0.Email;
DateLiked = user0.DateLiked;
Gender = user0.Gender;
ImageURL = user0.ImageURL;
else
throw new ApplicationException();
//LOCATION...
//LocationID = locationID;
//FBID = facebookID;
//Latitude = latitude;
//Langitude = langitude;
//Description = description;
//IEnumerable<User> users = context.Users.Where(u => u.FacebookID == facebookID);
IEnumerable<Location> locations = context.Locations.Where(l => l.FacebookID == facebookID);
Location newLocation = new Location();
newLocation.FacebookID = locationID;
newLocation.Latitude = "";
newLocation.Langitude = "";
newLocation.Description = locationValue;
IGeoCoder geoCoder = new GoogleGeoCoder(GoogleAPIKey);
Address[] addresses = geoCoder.GeoCode(locationValue);
context.Locations.AddObject(newLocation);
context.SaveChanges();
if (Address= true)
langtitude = ;;
public string GoogleAPIKey
get
return WebConfigurationManager.AppSettings["GoogleAPIKey"];
【问题讨论】:
【参考方案1】:你的问题不是很清楚,但我想你想要这样的东西:
IGeoCoder geoCoder = new GoogleGeoCoder(GoogleAPIKey);
Address[] addresses = geoCoder.GeoCode(locationValue);
// I believe the API never returns a null reference...
if (addresses.Length != 0)
// Let's assume the first one is good enough
Address address = addresses[0];
Location location = address.Location;
// Use location.Latitude and location.Longitude
请注意,为了可测试性,最好注入 IGeoCoder
而不是在您的类中创建它。
【讨论】:
它现在可以工作,但由于某种原因,它在调试时不断将相同的数据添加到数据库中。我怎么能阻止这个?我觉得这条线可能有问题; IEnumerable以上是关于如何从C#中的地址获取纬度和经度的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 ionic 中的 java 脚本从谷歌地图中的地址获取经度和纬度
如何从 windows phone 中的纬度和经度值获取地址?
如何在不使用 Xcode、iPhone 中的谷歌地图的情况下从地址获取纬度和经度