Baidu map, click the map to get the latitude and longitude, And the location to get the latitude and longitude

Source: Internet
Author: User

Baidu map, click the map to get the latitude and longitude, And the location to get the latitude and longitude

In the past two days, I mainly went to Baidu map. At the beginning, I was confused about what covered things and what points of interest .....

So this article will introduce some basic knowledge, covering things on a map, such

Interest points are the places in circles, including restaurants and buildings.

 

Okay, then let's talk about the map. First, you must get a KEY. Because there is no such thing, you won't be able to load the map... I'm so lazy and go around a big circle. Apply for a project and check the official website.

 

Then you can go to the next official Demo and watch the API change its code. Demo: http://lbsyun.baidu.com/sdk/download? Qq-pf-to = pcqq. C2C

 

Well, paste the code. Enter the longitude and latitude, and set a position. You can click the attachment. Obtain the longitude and latitude of the click

. Java

 

Package com. example. mybaidumap;


Import android. app. Activity;
Import android. content. Intent;




Import android. OS. Bundle;
Import android. util. Log;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. TextView;
Import android. widget. Toast;


Import com. baidu. location. BDLocation;
Import com. baidu. location. BDLocationListener;
Import com. baidu. location. LocationClient;
Import com. baidu. location. LocationClientOption;
Import com. baidu. mapapi. SDKInitializer;
Import com. baidu. mapapi. map. BaiduMap;
Import com. baidu. mapapi. map. BaiduMap. OnMapClickListener;
Import com. baidu. mapapi. map. BitmapDescriptor;
Import com. baidu. mapapi. map. BitmapDescriptorFactory;


Import com. baidu. mapapi. map. MapController;
Import com. baidu. mapapi. map. MapPoi;
Import com. baidu. mapapi. map. MapStatusUpdate;
Import com. baidu. mapapi. map. MapStatusUpdateFactory;
Import com. baidu. mapapi. map. MapView;
Import com. baidu. mapapi. map. MarkerOptions;
Import com. baidu. mapapi. map. MyLocationData;
Import com. baidu. mapapi. map. OverlayOptions;
Import com. baidu. mapapi. model. LatLng;
Import com. baidu. mapapi. search. core. SearchResult;
Import com. baidu. mapapi. search. geocode. GeoCodeOption;
Import com. baidu. mapapi. search. geocode. GeoCodeResult;
Import com. baidu. mapapi. search. geocode. GeoCoder;
Import com. baidu. mapapi. search. geocode. OnGetGeoCoderResultListener;
Import com. baidu. mapapi. search. geocode. ReverseGeoCodeOption;
Import com. baidu. mapapi. search. geocode. ReverseGeoCodeResult;
Import com. baidu. nplatform. comapi. basestruct. GeoPoint;
Import com. baidu. mapapi. map. LocationData;
Import com. baidu. mapapi. map. MapController;
// Baidu map. Required parameter: longitude and latitude. You can click it. The result is: Address, longitude and latitude.
Public class MainActivity extends Activity implements
OnGetGeoCoderResultListener, OnClickListener {
MapView mMapView = null;
BaiduMap mBaiduMap;
Boolean isFirstLoc = true;
Private String address;
GeoCoder mSearch;
Private TextView conaddress;
Private MapController mMapController = null;
LatLng latlng;
Private Button OK;
Double lon, lat;



@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );

SDKInitializer. initialize (getApplicationContext ());
SetContentView (R. layout. activity_main );
// Initialization. lat and lon are longitude and latitude values.
OK = (Button) findViewById (R. id. OK );
OK. setOnClickListener (this );
Conaddress = (TextView) findViewById (R. id. conaddress );
Lat = 39.904965;
Lon = 116.327764;
Latlng = new LatLng (lat, lon );

// Obtain map Control Reference
MMapView = (MapView) findViewById (R. id. bmapView );

MBaiduMap = mMapView. getMap ();
MSearch = GeoCoder. newInstance ();
// Set the initial Zoom size of the map.
MBaiduMap. setMapStatus (MapStatusUpdateFactory. zoomTo (17366f ));
// Enable the positioning Layer
// MBaiduMap. setMyLocationEnabled (true );


// Initialize the search module and register the event listener
MSearch = GeoCoder. newInstance ();
MSearch. setOnGetGeoCodeResultListener (this );
MSearch. reverseGeoCode (new ReverseGeoCodeOption ()
. Location (latlng ));



LocationClientOption option = new LocationClientOption ();
Option. setOpenGps (true); // enable gps
Option. setCoorType ("bd09ll"); // sets the coordinate type.
Option. setScanSpan (1000 );
Option. setAddrType ("all ");

OnMapClickListener listener = new OnMapClickListener (){
/**
* Map Click Event Callback Function
*
* @ Param point, poi
* Geographical coordinates of clicks, points of interest
*/



@ Override
Public void onMapClick (LatLng point ){
// TODO Auto-generated method stub
LatLng ptCenter = point;
// Anti-Geo search
MSearch. reverseGeoCode (new ReverseGeoCodeOption ()
. Location (ptCenter ));
PopWin (point );
Lon = point. longpolling;
Lat = point. latitude;
}


@ Override
Public boolean onMapPoiClick (MapPoi poi ){
// TODO Auto-generated method stub
Conaddress. setText (poi. getName ());
Log. w ("tag", poi. getName ());
PopWin (poi. getPosition ());
Lat = poi. getPosition (). latitude;
Lon = poi. getPosition (). longpolling;
Return false;

}
};
MBaiduMap. setOnMapClickListener (listener );
}


@ Override
Protected void onDestroy (){
Super. onDestroy ();
// Execute mMapView. onDestroy () When onDestroy is executed in the activity to implement map lifecycle management.
MMapView. onDestroy ();
MSearch. destroy ();
}


@ Override
Protected void onResume (){
Super. onResume ();
// Execute mMapView. onResume () When onResume is executed in the activity to implement map lifecycle management
MMapView. onResume ();
}


@ Override
Protected void onPause (){
Super. onPause ();
// Execute mMapView. onPause () When onPause is executed in the activity to implement map lifecycle management.
MMapView. onPause ();
}




// Custom prompt icon market
Public void popWin (LatLng point ){
// Construct the Marker icon
BitmapDescriptor bitmap = BitmapDescriptorFactory
. FromResource (R. drawable. nav_turn_via_1 );
// Construct a MarkerOption to add a Marker to a map
OverlayOptions option = new MarkerOptions (). position (point)
. Icon (bitmap );
// Add a Marker to the map and display it
MBaiduMap. clear ();
MBaiduMap. addOverlay (option );
}
// Address Encoding
@ Override
Public void onGetGeoCodeResult (GeoCodeResult result ){
If (result = null | result. error! =


SearchResult. ERRORNO. NO_ERROR ){
Toast. makeText (MainActivity. this, "Sorry, The result cannot be found ",


Toast. LENGTH_LONG)
. Show ();
Return;
}
MBaiduMap. clear ();
MBaiduMap. addOverlay (new MarkerOptions (). position


(Result. getLocation ())
. Icon (BitmapDescriptorFactory
. FromResource


(R. drawable. nav_turn_via_1 )));
MBaiduMap. setMapStatus (MapStatusUpdateFactory. newLatLng (result
. GetLocation ()));
String strInfo = String. format ("latitude: % f longitude: % f ",
Result. getLocation (). latitude,


Result. getLocation (). longpolling );

Toast. makeText (MainActivity. this, strInfo,


Toast. LENGTH_LONG). show ();
Conaddress. setText (result. getAddress ());
}
// Edit
@ Override
Public void onGetReverseGeoCodeResult (ReverseGeoCodeResult result ){
If (result = null | result. error! =


SearchResult. ERRORNO. NO_ERROR ){
Toast. makeText (MainActivity. this, "Sorry, The result cannot be found ",


Toast. LENGTH_LONG)
. Show ();
Return;
}
MBaiduMap. clear ();
MBaiduMap. addOverlay (new MarkerOptions (). position


(Result. getLocation ())
. Icon (BitmapDescriptorFactory
. FromResource


(R. drawable. nav_turn_via_1 )));
MBaiduMap. setMapStatus (MapStatusUpdateFactory. newLatLng (result
. GetLocation ()));
Conaddress. setText (result. getAddress ());


}
// Click "OK" to upload the value
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Int id = v. getId ();
If (id = R. id. OK)
{
If (conaddress. getText ()! = Null ){
Intent intent = new Intent (MainActivity. this, Resultt. class );
Intent. putExtra ("address", conaddress. getText ());
Intent. putExtra ("lat", lat + "");
Intent. putExtra ("lon", lon + "");
StartActivity (intent );
}
}

}


}

 

 

 

. Xml:

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: paddingBottom = "@ dimen/activity_vertical_margin"
Android: paddingLeft = "@ dimen/activity_horizontal_margin"
Android: paddingRight = "@ dimen/activity_horizontal_margin"
Android: paddingTop = "@ dimen/activity_vertical_margin"
Tools: context = ". MainActivity">
<FrameLayout
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
>

<RelativeLayout
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
>

<Com. baidu. mapapi. map. MapView
Android: id = "@ + id/bmapView"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: clickable = "true"/>


<TextView
Android: id = "@ + id/conaddress"
Android: layout_width = "fill_parent"
Android: gravity = "left | center_vertical"
Android: paddingLeft = "10dp"
Android: layout_height = "40dp"
Android: textSize = "13sp"
Android: background = "#45000000"
Android: layout_alignParentTop = "true"
Android: textColor = "# FFFFFF"
/>


<Button
Android: id = "@ + id/OK"
Android: layout_width = "wrap_content"
Android: layout_height = "40dp"
Android: layout_alignBottom = "@ + id/conaddress"
Android: layout_alignParentRight = "true"
Android: layout_alignParentTop = "true"
Android: textSize = "20dip"
Android: text = "OK"/>


</RelativeLayout>

</FrameLayout>
</RelativeLayout>

Source code: http://www.jinhusns.com/Products/Download

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.