Android Gold map positioning, point marking, POI

Source: Internet
Author: User

The display of the blue dot after the API5.0 version of the gold map does not depend on the positioning API.

Package com.example.dell.a3dmap; Import Android.
Manifest;
Import Android.content.Context;
Import Android.content.pm.PackageManager;
Import Android.location.Geocoder;
Import android.location.Location;
Import Android.location.LocationManager;
Import Android.support.v4.app.ActivityCompat;
Import Android.support.v4.content.ContextCompat;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.View;
Import Android.widget.Button;

Import Android.widget.Toast;
Import com.amap.api.location.AMapLocation;
Import com.amap.api.location.AMapLocationClient;
Import Com.amap.api.maps.AMap;
Import Com.amap.api.maps.AMapUtils;
Import Com.amap.api.maps.LocationSource;
Import Com.amap.api.maps.MapView;
Import com.amap.api.maps.model.CameraPosition;
Import Com.amap.api.maps.model.LatLng;
Import Com.amap.api.maps.model.Marker;
Import com.amap.api.maps.model.MarkerOptions;
Import Com.amap.api.maps.model.MyLocationStyle; Import Com.amap.api.maps. Model.
Poi;
Import Com.amap.api.services.core.LatLonPoint;
Import Com.amap.api.services.core.PoiItem;
Import Com.amap.api.services.geocoder.GeocodeQuery;
Import Com.amap.api.services.geocoder.GeocodeResult;
Import Com.amap.api.services.geocoder.GeocodeSearch;
Import Com.amap.api.services.geocoder.RegeocodeQuery;

Import Com.amap.api.services.geocoder.RegeocodeResult;

Import java.util.List; public class Mainactivity extends Appcompatactivity implements Amap.onmylocationchangelistener {private Mapview MAPV
    Iew;
    AMap AMap;
    Mylocationstyle Mylocationstyle;
    marker[] Marker = new MARKER[10];
    int totalmarker;

    int Write_coarse_location_request_code;
        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
        Setcontentview (R.layout.activity_main);
        Initialize the map control Mapview = (Mapview) Findviewbyid (R.ID.MAP);
        Must write Mapview.oncreate (savedinstancestate);
    Totalmarker = 0;    AMAP = Mapview.getmap ();
        Mylocationstyle = new Mylocationstyle ();
        Mylocationstyle.mylocationtype (mylocationstyle.location_type_show);
        Amap.setmylocationstyle (Mylocationstyle);
        Amap.getuisettings (). Setmylocationbuttonenabled (True);
        Amap.setmylocationenabled (TRUE);
        Amap.setonmylocationchangelistener (this); Amap.setonmaplongclicklistener (New Amap.onmaplongclicklistener () {@Override public void Onmaplong
                Click (Final latlng latlng) {latlng mylatlng;
                MYLATLNG = new Latlng (Amap.getmylocation (). Getlatitude (), Amap.getmylocation (). Getlongitude ());
                float dis = amaputils.calculatelinedistance (mylatlng, LATLNG);
                Marker[totalmarker] = Amap.addmarker (new markeroptions (). Position (LATLNG). Title (""). Snippet ("Straight Distance: + dis");
                Final Geocodesearch geocodesearch = new Geocodesearch (Getapplicationcontext ()); Geocodesearch.setongeoCodesearchlistener (New Geocodesearch.ongeocodesearchlistener () {@Override public
                            void Onregeocodesearched (Regeocoderesult regeocoderesult, int i) {if (i = = 0) { System.out.println ("I=0!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                        \ n ");
                            else {list<poiitem> poiitemlist;
                            Poiitemlist = Regeocoderesult.getregeocodeaddress (). Getpois (); Marker[totalmarker].settitle (Regeocoderesult.getregeocodeaddress (). Getdistrict () + Getnearestname (poiItemList,
                            LATLNG) + "nearby"); System.out.println (Regeocoderesult.getregeocodeaddress (). Getcity () + <<<<<<<<<<
                            <<<<<<<< "); System.out.println (Regeocoderesult.getregeocodeaddress (). Getdistrict () + <<<<<<<<< <<<<<<<<<<< ");
                        totalmarker++; @Override public void ongeocodesearched (Geocoderesult Geo
                Coderesult, int i) {}});
                Latlonpoint latlonpoint = new Latlonpoint (latlng.latitude, latlng.longitude);
                Regeocodequery query = new Regeocodequery (Latlonpoint, Geocodesearch.amap);
            Geocodesearch.getfromlocationasyn (query);
        }
        }); Amap.setonmarkerclicklistener (New Amap.onmarkerclicklistener () {@Override public boolean Onmarker
                Click (Marker Marker) {if (!marker.isinfowindowshown ()) {Marker.showinfowindow ();
                else {Marker.hideinfowindow ();
            return true;
    }
        }); The/** * method must be rewritten/@Override protectEd void Onresume () {super.onresume ();
    Mapview.onresume ();
        The/** * method must override/@Override protected void OnPause () {super.onpause ();
    Mapview.onpause (); The/** * method must override/@Override protected void Onsaveinstancestate (Bundle outstate) {Super.on
        Saveinstancestate (outstate);
    Mapview.onsaveinstancestate (outstate);
        The/** * method must override/@Override protected void OnDestroy () {Super.ondestroy ();
    Mapview.ondestroy ();
        @Override public void Onmylocationchange (Location Location) {latlng mylatlng;
        MYLATLNG = new Latlng (Location.getlatitude (), Location.getlongitude ()); for (int i = 0; I <= totalmarker i++) {Float dis = amaputils.calculatelinedistance (mylatlng, marker[i].ge
            Tposition ());
        Marker[i].setsnippet ("Linear Distance:" + string.valueof (dis)); } public String Getnearestname (list<Poiitem> poiitemlist, latlng targetlocation) {Double Mindis = +, Nowdis;
        String ret = "";
            for (int i = 0; I <= poiitemlist.size ()-1; i++) {Poiitem poiitem;
            Poiitem = Poiitemlist.get (i);
            LATLNG poilatlng = new Latlng (Poiitem.getlatlonpoint (). Getlatitude (), Poiitem.getlatlonpoint (). GetLongitude ());
            Nowdis = Amaputils.calculatelinedistance (targetlocation, POILATLNG);
                if (Nowdis < Mindis) {Mindis = Nowdis;
            ret = poiitem.tostring ();
    } return ret; }
}

The above code to achieve the display map, display positioning, long according to a map of a point to draw a point mark, the point of information contained in the current position distance point mark Distance, has recently been POI geographic description information.

Because the Regeocoderesult.getregeocodeaddress (). Getpois () function returns a list of POI that is queried for all poi within a certain range (for example, 500 m here), to return a recent address description information, This is done in the Getnearestname () method, where the first parameter is the list to be processed, the second is the location where the user clicks, the distance from all points in the list and the location of the click, and the smallest one as the POI description information.

I think these days, the main harvest of the API of the gold map learned to read the official document, in the absence of visual learning methods such as video, the official document is the only reference, but also the best reference. The above specific methods and properties can be found in the documents of the Gould detailed description.

The effect is as follows (is not exposed to my position Orz):




Related Article

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.