LBs Approaching alert

Source: Internet
Author: User
Tags textout
Project directory

 

Class Code

 

Mylocation. Java

 

Package com. Swift. mylocation;

Import java. Io. ioexception;
Import java. util. List;
Import java. util. locale;

Import Android. App. pendingintent;

Import Android. content. context;
Import Android. content. intent;

Import Android. Location. address;
Import Android. Location. criteria;
Import Android. Location. geocoder;
Import Android. Location. location;
Import Android. Location. locationlistener;
Import Android. Location. locationmanager;
Import Android. OS. Bundle;

 

Import Android. widget. textview;
Import Android. widget. Toast;

Import com. Google. Android. Maps. geopoint;
Import com. Google. Android. Maps. mapactivity;
Import com. Google. Android. Maps. mapcontroller;
Import com. Google. Android. Maps. mapview;
Import com. Google. Android. Maps. overlay;
Import com. Swift. mylocation. R;

Public class mylocation extends mapactivity {

Mapcontroller;
List <overlay> overlays;
Mypositionoverlay positionoverlay;
Locationmanager;
Mapview mymapview;

Final string proximity_alert = new string (
"Android. Intent. Action. proximityalert ");

@ Suppresswarnings ("deprecation ")
@ Override
Public void oncreate (bundle icicle ){
Super. oncreate (icicle );
Setcontentview (R. layout. Main );

Mymapview = (mapview) findviewbyid (R. Id. mymapview );

Mapcontroller = mymapview. getcontroller ();

Mymapview. setsatellite (true );
Mymapview. setstreetview (true );
Mymapview. displayzoomcontrols (false );

Mapcontroller. setzoom (17 );

Positionoverlay = new mypositionoverlay ();
Overlays = mymapview. getoverlays ();
Overlays. Add (positionoverlay );

Criteria = new criteria ();
Criteria. setaccuracy (criteria. accuracy_fine );
Criteria. setaltituderequired (true );
Criteria. setbearingrequired (false );
Criteria. setcostallowed (false );
Criteria. setpowerrequirement (criteria. power_low );
Locationmanager = (locationmanager) getsystemservice (context. location_service );

String provider = locationmanager. getbestprovider (criteria, true );

Location = locationmanager. getlastknownlocation (provider );

Updatewithnewlocation (location );

Locationmanager. requestlocationupdates (provider, 2000, 10,
Locationlistener );
 
Setproximityalert ();

}

Private Final locationlistener = new locationlistener (){
@ Override
Public void onlocationchanged (location ){
Updatewithnewlocation (location );
}

@ Override
Public void onproviderdisabled (string provider ){
Updatewithnewlocation (null );
}

@ Override
Public void onproviderenabled (string provider ){
}

@ Override
Public void onstatuschanged (string provider, int status, bundle extras ){
}
};

 
 

Void setproximityalert (){

Double lat = 31.620356666666666;
Double lng = 121.38631333333333;
Float radius = 50f;
Long expiration =-1;

Intent intent = new intent (proximity_alert );
Intent. setaction (proximity_alert );
Pendingintent proximityintent = pendingintent. getbroadcast (this,-1,
Intent, 0 );

Locationmanager. addproximityalert (Lat, LNG, radius, expiration,
Proximityintent );

}

/** Update UI with a new location */
Private void updatewithnewlocation (location ){

String latlongstring;
Textview mylocationtext;
Mylocationtext = (textview) findviewbyid (R. Id. mylocationtext );
String addressstring = "no address Found ";
If (location! = NULL ){

Positionoverlay. setlocation (location );

Double geolat = location. getlatitude () * 1e6;
Double geolng = location. getlongdistance () * 1e6;
Geopoint point = new geopoint (geolat. intvalue (), geolng. intvalue ());
Mapcontroller. animateto (point );

Double lat = location. getlatitude ();
Double lng = location. getlong.pdf ();
Latlongstring = "Lat:" + lat + "\ nlong:" + LNG;
Double latitude = location. getlatitude ();
Double longpolling = location. getlongpolling ();

Geocoder GC = new geocoder (this, locale. China );
Try {
List <address> addresses = GC. getfromlocation (latitude,
Longpolling, 1 );
Stringbuilder sb = new stringbuilder ();
If (addresses. Size ()> 0 ){
Address = addresses. Get (0 );
For (INT I = 0; I <address. getmaxaddresslineindex (); I ++)
SB. append (address. getaddressline (I). append ("\ n ");
SB. append (address. getcountryname ());
SB. append (address. getlocality (). append ("\ n ");
}
Addressstring = sb. tostring ();
} Catch (ioexception e ){
}
} Else {
Latlongstring = "no location found ";
}
Mylocationtext. settext ("your current position is: \ n" + latlongstring
+ "\ N" + addressstring );

Toast. maketext (this, this. getintent (). getstringextra (proximity_alert), Toast. length_long );
}

@ Override
Protected Boolean isroutedisplayed (){
Return true;
}
}

 

Mypostionoverlay. Java

 

Package com. Swift. mylocation;

Import Android. Graphics. Canvas;
Import Android. Graphics. paint;
Import Android. Graphics. Point;
Import Android. Graphics. rectf;
Import Android. Location. location;

Import com. Google. Android. Maps. geopoint;
Import com. Google. Android. Maps. mapview;
Import com. Google. Android. Maps. overlay;
Import com. Google. Android. Maps. projection;

Public class mypositionoverlay extends overlay {

Location, mlocation;
Final double lat = 31.620356666666666*1e6,
Lon = 121.38631333333333*1e6;
Final int CCC = 50;
Private Final int mradius = 5;

/**
* @ Return the location
*/
Public location getlocation (){
Return location;
}

/**
* @ Param location
* The location to set
*/
Public void setlocation (location ){
This. Location = location;
}

@ Override
Public void draw (canvas, mapview, Boolean shadow ){
Projection projection = mapview. getprojection ();
If (shadow = false ){

Double latitude = location. getlatitude () * 1e6;
Double longpolling = location. getlongpolling () * 1e6;
Geopoint, GP;
Geopoint = new geopoint (latitude. intvalue (), longpolling. intvalue ());
Gp = new geopoint (lat. intvalue (), Lon. intvalue ());

Point point = new point ();
Projection. topixels (geopoint, point );
Rectf oval = new rectf (point. X-mradius, point. Y-mradius,
Point. x + mradius, point. Y + mradius );

Paint paint = new paint ();
Paint. setargb (250,255, 0, 0 );
Paint. setantialias (true );
Paint. setfakeboldtext (true );
Paint backpaint = new paint ();
Backpaint. setargb (175, 50, 50, 50 );
Backpaint. setantialias (true );
Rectf backrect = new rectf (point. x + 2 + mradius, point. Y-3
* Mradius, point. x + 65, point. Y + mradius );

Point center = new point ();

Projection. topixels (GP, center );

Paint CCP = new paint ();
CCP. setargb (250, 0, 0,255 );
CCP. setantialias (true );

Canvas. drawcircle (center. X, Center. Y, CCC, CCP );

Canvas. drawoval (oval, paint );
Canvas. drawroundrect (backrect, 5, 5, backpaint );
Canvas. drawtext ("here I am", point. x + 2 * mradius, point. Y, paint );

}
Super. Draw (canvas, mapview, shadow );
}

@ Override
Public Boolean ONTAP (geopoint point, mapview ){
Return false;
}
}

 

Proximityintentreceiver. Java

 

Package com. Swift. mylocation;

Import Android. content. broadcastreceiver;
Import Android. content. context;
Import Android. content. intent;
Import Android. Location. locationmanager;
Import Android. util. log;

Public class proximityintentreceiver extends broadcastreceiver {

Private final string textin = "alert: In", textout = "alert: Out ";

@ Override
Public void onreceive (context, intent ){
String key = locationmanager. key_proximity_entering;
Boolean isenter = intent. getbooleanextra (Key, false );
If (isenter ){
Log. D ("Allan", textin );


} Else {
Log. D ("Allan", textout );



}

}

}

 

 

 

Main. xml

 

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Textview
Android: Id = "@ + ID/mylocationtext"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>


<Com. Google. Android. Maps. mapview
Android: Id = "@ + ID/mymapview"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: enabled = "true"
Android: clickable = "true"
Android: apikey = "yourapikey"
/>
</Linearlayout>

Androidmanifest. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "com. Swift. mylocation">
<Application Android: icon = "@ drawable/icon">
<Uses-library Android: Name = "com. Google. Android. Maps"/>
<Activity Android: Name = "com. Swift. mylocation. mylocation" Android: Label = "@ string/app_name">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>

<Cycler Android: Name = "proximityintentreceiver">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. proximityalert"/>
</Intent-filter>
</Cycler>

</Application>

<Uses-Permission Android: Name = "android. Permission. access_coarse_location"/>
<Uses-Permission Android: Name = "android. Permission. Internet"/>
<Uses-Permission Android: Name = "android. Permission. access_fine_location"/>
<Uses-SDK Android: minsdkversion = "3"/>
</Manifest>

 

 

 

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.