Baidu Map _api

Source: Internet
Author: User
Tags sha1

This day is mainly about the use of Baidu Map API, because Baidu has a very detailed development documentation, so I only do a general note

Use of Baidu Map API

1, Getting Started

1.1, three major core

①sdkinitializer the entire Baidu initialization tool class, engine

②mapview controls to display a map

③baidumap equivalent to controller, zoom, rotate, move

1.2, ready to work (to Baidu map lbs open platform to view)

Get API Key and follow the steps on the online Help document

Get SHA1 Code

Baidu map key and application are bound to each other, a key corresponding to an application.

1.3. Create a project copy jar package

Copy the note to copy the folder, this jar package must use the Arme processor emulator and mobile phone

X86 Mobile Phone This application is not able to run up (Baidu Limited, cannot solve)

The v7a is faster than the speed without v7a, and the phone will automatically match

Add permissions, copy the corresponding permissions file.

1.4 Use steps (there are help documents in the downloaded dependency package):

① layout file Add mapview Baidu custom controls.

Additional: Two sets of jar packages, can leave only a package without v7a, run faster

Error: The supplyed the global app context info from Sdkinitializer .... (Baidu engine is not initialized, you need to check the above Application API key)

Do not know how to search on Baidu search

>>>android SHA1 and Package acquisition methods

Keytool-list-v-keystore Debug.keystore

② in the manifest file, create a sub-label in Application <meta-data,name,value (key)/>

③ in the activity of using map,

Call Sdkinitializer.initialize (Getapplication (must be global context))

The return result here is achieved by broadcasting, so write a broadcast receiver to determine the result.

Determine the result by the value of the received broadcast intent.getaction (no network, checksum failure, etc.)

The action of the intent filter can be set by Sdkinitializer.xxxx when the code is registered.

1.5 zoom level, set center point

Settings for the zoom level:

The ① zoom level is 3-19 (V2. X used to be 3-18).

The difference between 2.x and 1.x

Modified the format of the map file, 1.x map with grid version, 2.x map using vector version, optimized the data

Example: Beijing map 110m>>>15m

Added a level (18>>19 with 3D effect)

②BAIDUMAP Controller: Manage a specific Mapview: Select, scale, move

Baidumap Baidumap = Mapview.getmap ()//Locate the controller based on the object of the Mapview control

Baidumap.setmapstatus (mapstatusupdate)//change the current map state

Set Zoom to level 15, Baidu map default Level 12

Mapstatusupdate update = Mapstatusupdatefactory.zoomto (15);

③ Set Center point: The default is Tiananmen

Baidumap.setmapstatus (Mapstatusupdate)

Mapstatusupdate update = MAPSTATUSUPDATEFACTORY.NEWLATELNG (LATLNG)

LATLNG latlng =new latlng (longitude, dimension);//Baidu maps using latitude

2, three core classes (MVC)

There are two methods in 2.1 Sdkinitializer

① initializes the global, ② initializes the global and specifies the location of the storage

2.2 Mapview displays a view of the map, which must be manipulated according to its life cycle.

① must bind the activity's life cycle, or it may be a bug if it is minimized and returned.

②onresume (), OnPause (), ondestory () are bound to the life-cycle method that the activity corresponds to.

It is inherited ViewGroup, has the ViewGroup method.

You can specify the position of the scale control, set the position of the zoom control, display the scale bar, zoom the control, and so on, and view the API.

2.3 Baidumap

① has a bunch of interfaces for callbacks that listen to various events on the map

② method: Load the hot map, update the map status (draw, you can specify the time-consuming animation), etc., to view the API can be.

③ Main operation:

Zoom level: Baidumap.setmapstatus (mapstatus (increases the zoom level by specifying the object information returned by the factory))

Rotation: Rotates clockwise, centered on itself.

3D Stereoscopic chart with XYZ axis (overhead, upward rotation, essentially rotating on an axis);

Rotate at one point to the center

Mapstatus = Baidumap.getmapstatus ()//Get the current map status

Float rotate = mapstatus.rotate;//Get the rotation angle of the map

Update rotation angle

Baidumap.setmapstatus (Rotatemapstatu);

Mapstatusupdate Rotatemapstatu = Mapstatusupdatefactory.newmapstatus (rotatestatus)

Mapstatus rotatestatus = new Mapstatus.builder (). Rotate (0-360°). build ();

Rotate the axis in a straight line (overlooking)

Mapstatus = Baidumap.getmapstatus ()//Get the current map status

Mapstatus.overlook//0~45°

The update axis rotation is similar to the point rotation effect.

Move, similar to the reference Set center point (driving the picture has a process, without the process is to jump directly past)

Baidumap.animatemapstatus (through the factory. New Latlang (latitude, longitude));

3, Baidu Map Introduction Summary:

Use of three core classes

Get Apikey, get SHA1 value: Via DOS command, through eclipse

Copy jar package and permissions, X86 processor is not available.

Load Mapview control, remember to initialize, verify key (called Before Setcontentview (view))

Configure the Baidu Map Apikey node in the manifest file

Control management: Scale, center position, move, rotation angle,

Settings for Compass: uisettings (). setcompassenabled (FALSE)//settings not visible

Uisettings () can be obtained via baidumap.getuisettings ()//

You can also change the location to view the API.

Handling Map Events: Various event listeners for the Baidumap core class (double-click, overlay, click, etc.).

4, Baidu Map enhancement (map layer, overlay, search, offline map)

4.1 Map Layers

Category: Basemaps (showing basic map information), real-time traffic infographic, satellite map (real geographic appearance)

4.1.1 Map Layer Implementation steps (BASEMAP, traffic map, satellite map)

① Checksum key (if you do not care about returning broadcasts can not receive broadcasts), initialize, get baidumap, bind life cycle

② set 3 keys: onKeyDown (int keycode,keyevent event)//can also be set with a button

Toggle Layers:

Baidumap.setmaptype (BAIDUMAP.XXXX)//set the corresponding layer. Satellite map, General diagram

Baidumap.settrafficenadbled (TRUE)//Set traffic map

4.2 Overlay-Draw diagram (everything on the map overlays or overlays the contents of the map (such as a route))

Overlayoptions base class, this base class does not have a public method, it represents all operations on the same map.

1. Create your own overlay

2. Setting data for coverings (data set for different overlays)

3, add cover

4.2.1 Overlay Demo Sample

Create baseactivity load the basic information of all maps (check key, bind life cycle, get baidumap, etc.), or every little demo will be written again.

circleoptionsdemo//round cover, name optional

① Draw Circle Condition: Draw Circle, center + radius, fill + color + if circle border weight is not filled, style

② to perform the steps:

To create your own objects

Circleoptions circleoptions = new Circleoptions ();

Set up data;

Circleoptions.center (Hmpos)//Set the center, return the current object, you can use chained programming.

. RADIUS (1000)//RADIUS, Unit meter.

. FillColor (0x00 (transparency) 00 (red) 00 (green) 00 (blue))//Set color 16-in, transparency, red-green-blue

. Stroke (New Stroke (5 (width), color))//Set border

Add to map:

Baidumap.addoverlay (circleoptions);//The circle is displayed completely only when the circle can be fully displayed.

Textoptionsdemo

① Create Object textoptions.

② set the data, according to the method on the API, set the effect you want to set.

Fonts under Android: Three symmetrical, asymmetric, equal-width (not satisfied words can be customized)

③ Add to map Baidumap.addoverlay (textoptions)

Markeroptions (similar to a label, such as a supermarket around, a bank, a nail set, show the same type of things)

① creating objects

② setting Data: Set refresh time, icon (or icon set can be used to toggle), etc.

Marker.icon (Bitmapdescriptor)//Set the displayed picture

Via bitmapdescriptorfactory can be obtained by retrieving the resource feedback objects in various locations (SD card, id)

③ added to the map baidumap.addoverlay (markeroptions);

Multiple markeroptions tags can be added because multiple tags may need to be inserted

New once Markeroptions object, repeat the operation. (coordinates are not the same)

Icons (collection of bitmapdescriptor)//can set multiple pictures, will automatically switch, there are methods to set the interval.

. Draggable (TRUE)//can be dragged

Bubble Display: Demand: When you click on the overlay, the bubble pops up

Implementation step ① Pre-load pop, set to hidden

② when clicked, update location, set to show

This makes the user experience even better.

Mapview.addview (pop layout file, params)

The params here is mapviewlayoutparams.builder ()//through the parameter classes provided by Baidu to obtain parameters.

. LayoutMode (MapViewXXX.ElayoutMode.mapMode)//Set mode, via latitude or map mode

Map mode moves as the map moves, and the latitude is fixed in place

. Position (Hmpos)//Set location, cannot be null

. Width (mapviewxxx.wrap_content). Height (XXXXX)//Set wide height

. build ();

Set Up Click events

Baidumap.setonmarkerclicklistener (); coordinates can be obtained by overriding the parameters of the method

BUG: If there is a marker on the map with multiple images, it is possible to take the bubbles

The location of the bubble also has a way to set the offset (the y-axis is negative and downward is a positive number).

Other covering: slightly, use the time to directly view Baidu's help document (Chinese, easy to understand)

4.2.2 Search Cover

First, the core class of the search service is divided into two categories

① location Search, nearby search, range search

② bus Search, ride search, walk search

The result of their response: Overlaymanager (the core category of the search overlay)

POI (poing of interest points of interest)

Initiate scope search based on scope and search terms

Peripheral Search

City POI Search

POI Detail Search

Results show: Poioverlay

Example: Rectangle-Scoped Search, key class:

Poisearch Poisearch = Poisearch.newinstance ();//Gets the object that initiated the POI search request

Poisearch.setongetpoisearchresultlistener (New XXX)

Poiserarch.searchinbound (new XXX)//method of initiating the request

This xxx is used to set parameters, new out to view the API, per page capacity, pagination number, etc.

What parameters are needed for new parameters, and then see the help document to find the method you need.

Because the use of Baidu map is mainly API interface calls, so do not do detailed notes, most of the features to view the developer documentation provided by Baidu can be used, there is a need for special notes will be taken.

Special Annotations:

First click Rectangle Range Search No data appears, the second time comes out:

Verify that the key and the code initiating the search request are asynchronous, and that the test key may not be completed to initiate the request.

Search in the range, not just search within the scope of the search, but in the scope of the search for the center of the search.

Need to return the results you need to write a class to inherit Poioverlay, overriding the Onpoiclick () method

In this method, create the Poireuslt object, Poiresult.getallpoi ()//Get all the POI returned, through the parameters of the method can get the user specific click Poiinfo

Poinearby around search, steps are similar, but it retrieves a circle.

Perimeter search searches only for data within the center of the circle

Search within the city, steps similar to poisearch.searchincity (XXX);

Need to set up city

Set Paging information

Do not want two-page information overlay each other, each time the data before emptying baidumap.clear ()//clear the last data

Search Details: poisearch.searchpoidetail (poidetailsearchoption option)

Set UID Option.poiuid (POIINFO.UID)

Result processing of driving route

① creates an overlay that has its own covering drivingrouteoverlay

② Set Data SetData (Result.getroutelines (). Get (0)//If there is more than one data, select No. 0);

③ added to Data overlay.addtomap ();

Overlay.zommtospan ();//Display all in the screen

Drive route change icon and add Route point

Options.passby (nodes);//nodes is a collection of interfaces

Create class inheritance Drivingrouteoverlay, override two methods to modify icons

Other contents of bus transfer:

Address Resolution Service GEOCODER.XXX (address information can be obtained based on address name or geographic coordinate point)

Detailed retrieval of public transport information by Poiinfo.uid (depending on the type to determine whether it is a public interest point, and then passed to another method poisearchincity query bus line details)

Associative word retrieval: Send the Lenovo Word to the server, the server will return the corresponding associative words

Offline Map: Download the map package provided by Baidu, different versions placed in different SD card directory, and finally need a series of methods to load.

5. Positioning (GPS positioning, base station positioning (CELLID,LOC), WiFi location (MAC address, through the GPS or base station location of the information uploaded to the Baidu server, in the future can be directly through the MAC address of the router to obtain positioning information, domestic Baidu do better)):

The latitude and longitude obtained are encrypted again on the basis of Mars coordinates (which may be inaccurate on other map applications)

Location information is not found in the offline help document and can be seen online

Copy the sample code in the online document, change the copy

Positioning mode: ① high-precision network positioning and GPS positioning are used

② low-power, GPS-not-used

③ only uses the device location mode, does not need to connect the network, only uses the GPS location, does not support the indoor environment

Baidumap setmylocationdata (mylocationdata data)//Set the location, reference the Help document

Notice to stop when you start positioning (lose focus and stop)

Move focus to position when positioning

Baidu Map _api

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.