2011-09-28 171 views
0

我想根據地址獲取緯度和經度。我已經設置了它,以便捕捉用戶位置,並將其顯示爲曼徹斯特。如何從C#中的地址獲取緯度和經度

我想寫一個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"]; 
      } 

     } 

    } 

} 

回答

0

你的問題是不是真的不清楚,但我猜你想要的東西,如:

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內,而不是創建它的一個好主意,你的類。

+0

它現在可以工作,但由於某種原因,它會在調試過程中不斷向數據庫添加相同的數據。我怎樣才能阻止呢?我覺得這可能是這條線的問題; IEnumerable locations = context.Locations.Where(l => l.FacebookID == facebookID); –

+0

@PrinceJavaliciouz:聽起來這是一個完全不同的問題,說實話 - 應該在另一個問題中詢問*,理想情況下僅*代碼的相關位。 (我花了一段時間在你的問題中找到相關的代碼 - 請閱讀http://tinyurl.com/so-hints。) –

+0

抱歉,你之前發現代碼時遇到問題,我將其全部發布以防萬一其他人可能需要參考一切。我問了一個新問題。感謝您的鏈接。將有一個閱讀。 –