Use Baidu Locator API to get the current user login address

Source: Internet
Author: User

Recently in a mall project, customers want to make the site similar to the effect of the United States, change the region, the content also follow changes. This is the first to solve the problem of obtaining an address based on the user ID, and finally decided to use Baidu positioning (not for the project to build a reverse proxy)

String URL =request.getremoteaddr (); //url = "58.56.23.118"; //Local code test IP, using local public network//here call Baidu's IP Location API service see AK need to apply// http://api.map.baidu.com/lbsapi/cloud/ip-location-api.htmOrg.json.JSONObject json = Readjsonfromurl ("http://api.map.baidu.com/location/ip?ak= Mosaic part &ip=" +URL); //json.get ("Content")). Get ("address")); //status State is 2, indicating no access to region information                if((Integer) json.get ("status")! = 2) {                    /** Object adress = ((org.json.JSONObject) * Json.get ("content"). Get ("Address                     "); */                    //"Address_detail": {"province": "Shandong Province", "City": "Jinan", "Street": "", "District": "", "Street_number": "", "City_code" : 288}, "Address": "Jinan, Shandong Province", "Object adress =NULL; if(Json.get ("content")! =NULL&& ((org.json.JSONObject) json.get ("content"). Get ("Address_detail")! =NULL) {adress=(( Jsonobject) ((org.json.JSONObject) JSON. Get ("Content")). Get ("Address_detail")). Get ("City")); String AreaName=(String) adress; Get to City city name//request.getsession (). SetAttribute ("Area_name", areaname);MAP params =NewHashMap (); Params.put ("AreaName", AreaName); List<Area> areas = This. Areaservice. Query ("Select obj from area obj where Obj.areaname=:areaname", params,-1,-1); if(Areas.size () > 0 && Areas! =NULL) { Area area= Areas.get (0); area_id=Area.getid (). Longvalue (); Request.getsession (). SetAttribute ("AREA_ID", area_id); Request.getsession (). SetAttribute ("Area_name", AreaName); Mv.addobject ("AREA_ID", area_id); Mv.addobject ("Adress", adress); }                    }                                        Else                    {                        //mv.addobject ("Adress", "China Region");area_id =NULL; Request.getsession (). SetAttribute ("Area_name", "China region"); }

The above code has the original project code, positioning accurate to the city level, get the address when the default China region, in addition to the positioning part of the other ignore.

The tool class used in the above Code Readjsonfromurl ()

 Public Staticorg.json.JSONObject readjsonfromurl (String URL)throwsIOException, jsonexception {InputStream is=Newurl (url). OpenStream (); Try{BufferedReader rd=NewBufferedReader (NewInputStreamReader (IS, Charset.forname ("UTF-8"))); String Jsontext=ReadAll (RD); Org.json.JSONObject JSON=NewOrg.json.JSONObject (Jsontext); returnJSON; } finally{is.close (); //System.out.println ("also can see from here even return, still will execute finally!") ");        }    }

When the function is complete, login the homepage of the website to display the effect:



Use Baidu Locator API to get the current user login address

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.