Android GPS Positioning and example code _android

Source: Internet
Author: User
Tags drawtext

GPS positioning is a more interesting function of smart phones, lbs and other services are effective use of GPS positioning function. This article will share with you the knowledge of GPS positioning in Android development.

First, Android Basics preparation

1, Activity Class

Each mobile development environment has its own base class. The base class for the J2ME application is applets, and the base class of the Android program is the activity. This activity provides us with access to the basic functions and events of the mobile operating system. This class contains basic construction methods, keyboard processing, hanging up and resuming functions, and access to other low-level handheld devices. Essentially, our application will be an extension of an activity class. In this article, readers will learn how to use the activity class to write an Android program. The following is an example of a simple inheritance activity.

Java code

public class Locateme extends activity  
{public  
 void OnCreate (Bundle params)  
 {  
  super.oncreate (params) ;  
  Setcontentview (R.layout.main);  
 }  
   
 public boolean onKeyDown (int keycode, keyevent event)  
 {return  
  true;  
 }  
} 

The OnCreate method in the above program will be invoked the first time the application starts. The bundle object contains any basic information needed to establish parameters or environmental data. Activity can be full screen, or suspended. They can be nested, but each part is basically independent. Well, some might ask, what does Setcontentview do?

2. View class

The view class is a super class for Android, which contains almost all of the screen types. But there are some differences between them. Each view has a canvas for painting. This canvas can be used for any extension. For convenience, this article covers only two main view types: View and XML content that defines view and Android. In the above code, the "Hello World" XML View is used, which begins in a very natural way.
If we look at the new Android project, we'll find a file called Main.xml. In this file, a simple XML file is used to describe the layout of a screen. The contents of this simple XML file are as follows:

xml/html Code

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= 
"http://schemas.android.com/apk" /res/android " 
  android:orientation=" vertical " 
  android:layout_width=" fill_parent " 
  android:layout_" height= "Fill_parent" 
  > 
<textview 
android:layout_width= "fill_parent" 
android:layout_ height= "Wrap_content" 
android:layout_centerhorizontal= "true" 
android:text= "Press the center key to locate Yourself " 
/> 
</RelativeLayout> 

The functionality of the above looks very obvious. This special file defines a related layout, which means that it is described by the relationship of one element to another, or the relationship of their parent element. For a view, there are some ways to layout, but in this article you focus on the XML file.

The realtivelayout contains a text box that fills the entire screen (that is, our locateme activity). This locateme activity is full-screen by default, so the text box inherits this property and the text box appears in the upper-left corner of the screen. In addition, you must set a reference number for this XML file so that Android can find it in the source code. By default, these reference numbers are saved in R.java, and the code is as follows:

Java code

Public final class R   
{public   
 static final class layout   
 {public   
  static final int main=0x7f030001;   
 }   
}  

Views can also be nested, but unlike J2ME, we can use custom views with the widgets released by the Android team. In J2ME, developers were forced to select the Gamecanvas and J2ME application canvas. This means that if we want a custom effect, we have to redesign all of our widgets on the Gamecanvas. Android is more than that, and view types can be mixed. Android also brings together a widget library that includes scrollbars, text entities, progress bars, and many other controls. These standard widgets can be overloaded or customized according to our custom. Now let's get into our example.

Two, Android GPS positioning example

This demo application will demonstrate the current longitude and latitude of the user (shown in the text box). OnCreate construction method will be the same as the above example, in addition to adding keyboard processing, now let's look at the onkeydown code.

Java code

public boolean onKeyDown (int keycode, keyevent event)  
{  
 if (keycode!= keyevent.keycode_dpad_center | | m_ bloading)  
 {return  
  true;  
 }  
 M_bloading = true;  
 GetLocation ();  
 return true;  
} 

Let's explain this code, first of all, that the code checks for the key that is currently being pressed, but has not yet begun processing. But to deal with all this in the GetLocation method. Then, the flag flag is loaded and the GetLocation method is invoked, and the following is the code for the GetLocation method.

Java code

private void GetLocation ()  
{  
  Location loc;  
  Locationmanager Locman;  
  Locationprovider Locpro;  
  List<locationprovider> prolist;  
  Setcontentview (r.layout.laoding);  
  Locman = (Locationmanager) getsystemservice (location_service);  
  Prolist = Locman.getproviders ();  
  Locpro = prolist.get (0);  
  loc = Locman.getcurrentlocation (Locpro.getname ());  
  Lat = (float) loc.getlatitude ();  
  Lon = (float) loc.getlongitude ();  
  CreateView ();  
  Setcontentview (CustomView);  
} 

So far, the program is getting more interesting. But unfortunately, Google's documentation on the aspect is relatively small. After the variable declaration of the program, we need to demonstrate some load information. R.layout.loading conforms to another simple view of XML layout. By simply calling the Setcontentview method, you can redraw the screen using the reprint information.

The reader should note that at compile time, Android will wrap up all the XML layout data in advance. If we want to change the layout properties after compiling, we have to do this in the source program, as required.

The only way to get Locationmanager is through a call to the Getsystemservice () method. By using Locationmanager, we can get a list of location providers. In a real handheld device, this list includes some GPS services. In fact, we want to choose a more powerful, more accurate, and finally without other additional services to the GPS. Now, a GPS for testing is provided in the simulator, which comes from San Francisco. Custom GPS files can be uploaded and tested. If we were to test more complex applications, GPS from San Francisco might not be appropriate.

We can now use the Location Manager and the location provider for Getcurrentlocation calls. This method returns a snapshot of the current position of the machine, which is provided in the form of a location object. In a handheld device, we can get the longitude and latitude of the current position. Now, with this virtual handheld device, we can get the final result of this example program: A custom view is created.

Third, use the custom view

In the simplest form, an Android view only needs to overload a OnDraw method. A custom view can be a complex 3D implementation or a very simple text form. The following CreateView method lists what is seen above.

Java code

Public Voidcreateview () {   
   CustomView = Newcustomview (this);   
}  

This method simply invokes the construction method of the CustomView object. The CustomView class is defined as follows:

Java code

public class CustomView extendsview{   
   locateme overlord;   
 Publiccustomview (Locateme pCtx) {   
   super (PCTX);   
   Overlord = PCtx;   
 }   
 Public Voidondraw (Canvas CVS) {   
  Paint p = newpaint ();   
  String sLat = "Latitude:" + Overlord.getlat ();   
  String SLon = "Longitude:" + Overlord.getlon ();   
  Cvs.drawtext (SLat, p);   
  Cvs.drawtext (SLon, MB, p);   
 }   
 

This custom Android view gets the test data for longitude and violation and displays the data on the screen. This requires a pointer to Locateme, the activity class being the core of the entire application. Its two methods are the construction method and the OnDraw method. This constructor invokes the constructor method of the superclass and the interrupt that caused the activity pointer. The OnDraw method creates a new paint object that encapsulates color, transparency, and other topic information, and the object will access the color theme. In this program, you install the strings to display and draw them to the screen using the canvas pointer. This canvas looks very similar to the one we know about the J2ME game.

Thank you for reading this article, I hope to help the need for friends, thank you for your support for this site!

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.