Document Maps JavaScript API Reference Manual Geocoding CitySearch

CitySearch

Class name

Description

AMap.CitySearch

Return corresponding city information based on IP, providing the function to obtain city information by inputting IP or automatically obtaining IP. Users can retrieve and display query results through custom callback functions. If the service request fails, the system will return an error message.

CitySearchResult

CitySearchResult object

AMap.CitySearch 

Construct an IP location service object

new AMap.CitySearch()

Method

getLocalCity(cbk)

Automatically obtain the user's IP, and the callback returns the current city of the user. When status is complete, the result is CitySearchResult; when status is error, the result is error info; when status is no_data, it means the search returned 0 results

Parameter: 

cbk (function (status: String, result: info/CitySearchResult): Callback function

Demo:

citySearch.getLocalCity(function (status, result) {
  console.log(status, result);
  //status: complete indicates a successful query, no_data indicates no results found, error represents a query error
  //When the query is successful, the result is location-related information
});

getCityByIp(ip, CitySearchCallback)

Returns the corresponding city information based on the input IP address, status as above

Parameter: 

ip (String): IP address

CitySearchCallback: Callback function

Demo:

citySearch.getCityByIp("IP address", function (status, result) {
  //status: complete indicates a successful query, no_data indicates no results, and error represents a query error
  //When the query is successful, the result is the location-related information
});

Event

Name

Description

complete

This event is triggered when the query succeeds

error

This event is triggered when the query fails

CitySearchResult

CitySearchResult object(Type: Object

Attribute

Type

Description

city

String

City Name

bounds

Bounds

The rectangular area used to display the city on the map