android--Gold Map Automatic positioning

Source: Internet
Author: User

As with other maps, go to the official website to register as a developer and then get the key. Directly below the code.


Package Com.example.gaodemap;import Com.amap.api.maps.amap;import Com.amap.api.maps.cameraupdate;import Com.amap.api.maps.cameraupdatefactory;import Com.amap.api.maps.mapview;import Com.amap.api.maps.model.LatLng; Import Com.amap.api.maps.model.marker;import Com.amap.api.maps.model.markeroptions;import android.app.Activity; Import Android.content.context;import Android.location.location;import Android.location.locationlistener;import Android.location.locationmanager;import Android.os.bundle;import Android.widget.compoundbutton;import Android.widget.togglebutton;public class Mainactivity extends Activity {private Mapview mmapview;private AMap AMap; Private Mapview mapview;private Locationmanager locationmanager; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); LocationManager = (Locationmanager) Getsystemservice (context.location_service); Mmapview = (Mapview) Findviewbyid (R.ID.MAP); Mmapview.oncreate (SavediNstancestate); init ();//gprs provides positioning information change locationmanager.requestlocationupdates (Locationmanager.gps_provider, 300, 8, New Locationlistener () {@Overridepublic void onstatuschanged (String provider, int status, Bundle extras) {//TODO Auto-gen erated method stub} @Overridepublic void Onproviderenabled (String provider) {////Use the positioning information provided by GPRS to update the location updateposition ( Locationmanager.getlastknownlocation (provider));} @Overridepublic void onproviderdisabled (String provider) {//TODO auto-generated method stub} @Overridepublic void Onlocationchanged (location location) {//TODO auto-generated method Stubupdateposition (location);}}); ToggleButton TB = (ToggleButton) Findviewbyid (R.ID.TB); Tb.setoncheckedchangelistener (new Compoundbutton.oncheckedchangelistener () {@Overridepublic void oncheckedchanged (Compoundbutton Buttonview, Boolean isChecked) {//TODO auto-generated method stubif (isChecked) {amap.setmaptype (amap.map_type_satellite);} Else{amap.setmaptype (Amap.map_type_normal);}});} Initialize the AMAP object private void init () {if (aMap = = null) {AMap = Mmapview.getmap ();}} @Overrideprotected void OnDestroy () {//TODO auto-generated method Stubsuper.ondestroy (); Mmapview.ondestroy ();} @Overrideprotected void OnPause () {//TODO auto-generated method Stubsuper.onpause (); Mmapview.onpause ();} @Overrideprotected void Onresume () {//TODO auto-generated method Stubsuper.onresume (); Mmapview.onresume ();} @Overrideprotected void Onsaveinstancestate (Bundle outstate) {//TODO auto-generated method Stubsuper.onsaveinstancestate (outstate); mmapview.onsaveinstancestate (outstate);} private void Updateposition (location location) {latlng pos = new Latlng (Location.getlatitude (), Location.getlongitude () );//Create a cameraupdatecameraupdate cu = Cameraupdatefactory.changelatlng (POS) that sets the latitude and longitude;//update the display area of the map Amap.movecamera (CU) ;//Clear all marker and other coverings amap.clear ();//Create a Markeroptions object markeroptions markoptions = new Markeroptions (); Markoptions.position (POS);//Add markeroptions (actually add Marker) Marker Marker = Amap.addmarker (markoptions);}}

<?xml version= "1.0" encoding= "Utf-8"? ><framelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" Match_parent "> <com.amap.api.maps. Mapview android:id= "@+id/map" android:layout_width= "match_parent" android:layout_height= "match_parent "> </com.amap.api.maps.MapView> <togglebutton android:id=" @+id/tb "android:layout_width="         Wrap_content "android:layout_height=" wrap_content "android:gravity=" Top|right "android:textoff=" General map "        android:texton= "Satellite map" android:checked= "false" android:background= "@android: Color/transparent" /> <linearlayout android:layout_width= "fill_parent" android:layout_height= "Wrap_content" a ndroid:layout_gravity= "Bottom" android:orientation= "horizontal" > <button android:id= "@+id/ Near "Android:layout_width=" WRap_content "android:layout_height=" Wrap_content "android:text=" near "android:layout_weight=" 1 "android:back Ground= "@android: Color/transparent"/> <button android:id= "@+id/route" android:layout_width = "Wrap_content" android:layout_height= "wrap_content" android:text= "route" android:background= "@android: C Olor/transparent "android:layout_weight=" 1 "/> <button android:id=" @+id/my "Android: Layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "my" android:background = "@android: Color/transparent" android:layout_weight= "1"/> </LinearLayout></FrameLayout>


android--Gold Map Automatic positioning

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.