Show Map in Browser

Source: Internet
Author: User

In app development, we often embed a map to show a specific location, such as a map to show a specific location. Take Shanghai Xujiahui as an example, as shown in:


If we open it directly using the browser, refer to Http://developer.baidu.com/map/uri-introweb.htm

If we know the latitude and longitude (lat, LNG), then very convenient, we can use the following way to show

Intent Intent = new Intent ();

Intent. Setaction ("Android.intent.action.VIEW");

Uri Content_url = URI

. Parse ("http://api.map.baidu.com/marker?location=31.200209,121.443481&output=html");

Intent. SetData (content_url);

StartActivity (intent);

The results can also be displayed correctly. But more often than not know the exact latitude and longitude, only the specific place name, then we how to deal with it? Baidu Web API to help us think of these problems, we can first use http://api.map.baidu.com/geocoder?address= Shanghai Xujiahui &output= JSON method to get the latitude and longitude, and then use the way shown above. Note that we are using JSON, and if we use HTML display, the following is:

Intent Intent = new Intent ();

Intent. Setaction ("Android.intent.action.VIEW");

Uri Content_url = URI

. Parse ("http://api.map.baidu.com/geocoder?address= Shanghai Xujiahui &output=html");

Intent. SetData (content_url);

StartActivity (intent);

The results shown are:

If you need to get the actual map results above, you need to click on the map icon in the ellipse to enter the map mode and get:

This is troublesome and requires the user to click once to get the desired content.

So our approach is to first use asynchronous processing to obtain latitude and longitude, and then populate the above API to show the relevant content. The specific implementation code is as follows:

class Addrasyncextends asynctask<string, Void, string> {

String addr;

@Override

protected String Doinbackground (String ... params) {

addr = params[0];

return dogetgeoaddr (addr);

}

@Override

protected void onpostexecute (String addrurl) {

Intent Intent = new Intent ();

Intent. Setaction ("Android.intent.action.VIEW");

Uri content_url = URI. Parse (addrurl);

Intent. SetData (content_url);

StartActivity (intent);

}

}

Public String Dogetgeoaddr (Stringaddr) {

Try {

URL url = new url (getaddressjson(addr));

HttpURLConnection con = (httpurlconnection)URL. openconnection ();

String Result = Readstream (con. getInputStream ());

if (result = =null) {

return Getaddressurl (addr);

} Else {

Locgeoresult loc = new Gson (). Fromjson (result,

Locgeoresult. class);

if (Loc. status. Equalsignorecase ("OK")) {

return Getaddressdetailurl (

Loc. result. location. lat,Loc. result. location. LNG,

" My location ",addr);

}

}

} Catch (Exceptione) {

e. Printstacktrace ();

}

return Getaddressurl (addr);

}

Private String Readstream (InputStreamin) {

BufferedReader Reader = null;

StringBuilder Mbuilder =new StringBuilder ();

Try {

Reader = new bufferedreader (new InputStreamReader (in));

String line = "";

while (line =reader. ReadLine ()) = null) {

mbuilder. Append (line);

}

} Catch (IOExceptione) {

e. Printstacktrace ();

return null;

} finally {

if (reader ! =null) {

Try {

reader. Close ();

} Catch (IOExceptione) {

e. Printstacktrace ();

}

}

}

return mbuilder. toString (). Replace ("", "");

}

Public static String Getaddressurl (String addr) {

String strURL = "http://api.map.baidu.com/geocoder?address=%1$s&output=html";

return String. format (strurl,encodestr(addr));

}

Public static String Getaddressjson (String addr) {

String strURL = "Http://api.map.baidu.com/geocoder?address=%1$s&output=json";

return String. format (strurl,encodestr(addr));

}

Public static String Getaddressdetailurl (floatlat, floatLNG, string title, string addr) {

StringstrURL = "http://api.map.baidu.com/marker?location=%1$f,%2$f&title=%3$s&content=%4 $s &output=html ";

return String. format (strurl,lat, LNG, title, addr);

}

Public static String Encodestr (String str) {

Try {

return Urlencoder. encode (str,"UTF-8");

} Catch (unsupportedencodingexceptione) {

e. Printstacktrace ();

}

return null;

}

Among them Locgeoresult are as follows:

Public class Locgeoresult {

Public Stringstatus;

Public Locresultresult;

Public class Locresult {

Public Locdetaillocation;

Public int precise;

Public int confidence;

Public Stringlevel;

}

Public class Locdetail {

Public float LNG;

Public float lat;

}

}

Finally call new Addrasync (). Execute (STRADDR); Can. The results are as follows:

If you do not want to invoke the browser, it is also possible to embed webview in the page. Get specific URL implementations in the same way as above

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Show Map in Browser

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.