0

我想根据地址获取经纬度。我已经对其进行了设置,因此它可以捕获将其显示为曼彻斯特的用户位置。

我想写一个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"];
            }

        }

    }

}
4

1 回答 1

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最好在类中注入而不是创建它。

于 2011-09-28T10:55:41.203 回答