Reactnative Call Mobile Map (gold, Baidu) navigation Android

Source: Internet
Author: User

Due to the needs of the project, the third-party SDK package has a slightly larger volume, and after evaluation, it is decided to use the app as well as the Web map to navigate.

Ideas:

In the need to invoke the navigation interface through the native access to the current mobile phone available in the Navigation app assembly to return to the RN to select the call, if not installed any app, then directly request the browser to open the web version of Baidu map. Note that the author chose Baidu positioning SDK to obtain coordinates, and then through the online service of the company Baidu to convert the coordinates of the German coordinates.

Integration:

1. Get a list of available maps:

    /*** Baidu Map Package name*/     Public Static FinalString Package_name_bd_map = "Com.baidu.BaiduMap"; /*** German Map package name*/     Public Static FinalString Package_name_mini_map = "Com.autonavi.minimap"; @ReactMethod Public voidGetavailablemapnames (FinalCallback Callback) {Map<string, string> names =NewHashMap (); if(IsAvailable ( This. Getcurrentactivity (), Package_name_bd_map)) {Names.put (Package_name_bd_map,"Baidu Map"); }        if(IsAvailable ( This. Getcurrentactivity (), Package_name_mini_map)) {Names.put (Package_name_mini_map,"The map of the German");    } callback.invoke (jsonhelper.tojsonstring (names)); }
View Code

2. Select the map to open the navigation:

    /**     * @paramPkgname *@paramparam*/@ReactMethod Public voidOpennavmap (string pkgname, string param,FinalCallback Callback) {        if(IsBlank (param)) {Responsemodel Responsemodel=NewResponsemodel (); Responsemodel.seterrmsg ("Parameter is empty");            Callback.invoke (jsonhelper.tojsonstring (Responsemodel)); return; }        Try{jsonobject Data=Newjsonobject (param); String Originlat= Data.get ("Originlat"). toString (); String ORIGINLNG= Data.get ("ORIGINLNG"). toString (); String Destlat= Data.get ("Destlat"). toString (); String DESTLNG= Data.get ("DESTLNG"). toString (); if(IsBlank (pkgname)) {// Http://lbsyun.baidu.com/index.php?title=uri/api/webString url = "http://api.map.baidu.com/direction?origin=%s,%s&destination=%s,%s&region=%s&mode= driving&output=html&src=%s "; //Open Web pageIntent Intent =NewIntent (); Intent.setaction ("Android.intent.action.VIEW"); Uri contentURL= Uri.parse (String.Format (URL, Originlat, originlng, Destlat, DESTLNG, This. GetName (), This. GetName ()));                Intent.setdata (contentURL);  This. Getcurrentactivity (). StartActivity (Intent); return; } String tmpname=Pkgname.trim (); if(Tmpname.equals (Package_name_bd_map)) {Intent I1=NewIntent (); //Car NavigationI1.setdata (Uri.parse (String.Format ("baidumap://map/navi?location=%s,%s", Destlat, destlng));  This. Getcurrentactivity (). StartActivity (I1); } Else if(Tmpname.equals (Package_name_mini_map)) {// http://lbs.amap.com/api/amap-mobile/guide/android/navigationStringBuffer scheme =NewStringBuffer ("androidamap://navi?sourceapplication="). Append ( This. GetName ()); //if (! Textutils.isempty (Poiname)) {//stringbuffer.append ("&poiname="). Append (Poiname); // }                //Dev Required is offset (0:lat and Lon are already encrypted and do not require state-tested encryption; 1: Requires country-tested encryption)//Style Required navigation method (0 speed, 1 less cost, 2 short distance, 3 do not go high speed; 4 avoid congestion; 5//do not take high-speed and avoid charges, 6 do not go high-speed and avoid congestion, 7 evasion of charges and congestion, 8 do not take high-speed evasion fees and congestion))Scheme.append ("&lat="). Append (Destlat). Append ("&lon="). Append (DESTLNG). Append ("&dev="). Append (0). Append ("&style="). Append (0); Intent Intent=NewIntent ("Android.intent.action.VIEW", Uri.parse (scheme.tostring ())); Intent.setpackage ("Com.autonavi.minimap");  This. Getcurrentactivity (). StartActivity (Intent); }        } Catch(jsonexception e) {e.printstacktrace (); Responsemodel Responsemodel=NewResponsemodel (); Responsemodel.seterrmsg ("Data parsing Error");        Callback.invoke (jsonhelper.tojsonstring (Responsemodel)); }    }
View Code

Where the secondary check method is:

1     /**2 * Check if the specified software is installed on the phone3      * 4      * @paramContext5      * @paramPackageName6 * App Package name7      * @return8      */9     Private Static Booleanisavailable (Context context, String PackageName) {Ten         FinalPackagemanager Packagemanager =Context.getpackagemanager (); One  A         //Get package information for all installed programs -list<packageinfo> PackageInfos = packagemanager.getinstalledpackages (0); -  the         //the package name used to store all installed programs -List<string> Packagenames =NewArraylist<>(); -  -         //Remove the package name from pinfo and press it into the PName list +         if(PackageInfos! =NULL) { -              for(inti = 0; I < packageinfos.size (); i++) { +String Packname =Packageinfos.get (i). PackageName; A Packagenames.add (packname); at             } -         } -  -         //determine if there is a package name for the target program in Packagenames, there is true, no false -         returnPackagenames.contains (packagename); -}
Check if the specified software is installed on the phone

You can then display the list call code in the RN interface.

Reactnative Call Mobile Map (gold, Baidu) navigation Android

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.