Nowadays, mobile devices with GPS functions are becoming more and more common. GPS positioning systems can be used to precisely locate your current location, but since GPS receivers need to be aligned with the sky, therefore, indoor environments are useless.
Another effective way to find your location is to use the mobile phone base station. When the mobile phone is turned on, it will keep in touch with the surrounding base station. If you know the identity of these base stations, you can use a variety of databases (including the identity of the base station and their exact geographic location) to calculate the physical location of the mobile phone. The base station does not need satellites. Unlike GPS, it works in the same indoor environment. But it is not as accurate as GPS, and its precision depends on the density of the base station. It has the highest accuracy in base station-intensive areas.
Tip: No GPS receiver is configured for the first-generation iPhone. The base station method cannot be applied to the iPod touch because it is not a mobile phone.
The third method is to rely on Wi-Fi. When using this method, the device connects to the Wi-Fi network and determines the location by checking the data of the service provider. It neither depends on satellites nor on base stations, therefore, this method is effective for areas that can be connected to Wi-Fi networks, but its accuracy is also the worst among the three methods.
Locating framework Kernel
On the iPhone, Apple provides the positioning framework kernel to help you determine your physical location. The beauty of this framework is that it uses all the three methods mentioned above, which method is used is transparent to developers. Developers only need to specify the required precision, and the positioning kernel will determine the positioning result in the best way.
Are you surprised ?! The rest of this article will show you how this is done.
Obtain coordinates
Use xcode to create a new view-based applicationProgramProject, named lbs. in the new project, double-click the lbsviewcontroller. XIB file and edit it in the interface design tool. Fill the View window with the following components, as shown in 1.
L label
L textfield
Figure 1 Location view instance: Fill the window with label and textfiled
Right-click the Framework Group in xcode, select "add" * "existing framework", select "framework/corelocation. Framework", and add the following bold characters to the lbsviewcontroller. h file:Code:
Java code
-
- #Import<Uikit/uikit. h>
-
- #Import<Corelocation/corelocation. h>
-
- @ InterfaceLbsviewcontroller: uiviewcontroller
-
- <Cllocationmanagerdelegate> {
-
- Iboutlet uitextfield * latitudetextfield;
- Iboutlet uitextfield * longitudetextfield;
-
- Iboutlet uitextfield * accuracytextfield;
-
- Cllocationmanager * lm;
-
- }
-
- @ Property(Retain, nonatomic) uitextfield * latitudetextfield;
-
- @ Property(Retain, nonatomic) uitextfield * longitudetextfield;
-
- @ Property(Retain, nonatomic) uitextfield * accuracytextfield;
- @ End
To use the cllocationmanager class, you need to implement the cllocationmanagerdelegate protocol in your view controller class. You also need to create three egresses to connect the three textfiled views in the View window.
Return to the interface editor, click and drag the document owner project to the three textfield views, and select latitudetextfield, longitudetextfield, and accuracytextfield respectively.
Query the bold Section in the following code in the lbsviewcontroller. M file:
Java code
-
- #Import "Lbsviewcontroller. H"
-
- @ ImplementationLbsviewcontroller
-
- @ SynthesizeLatitudetextfield, longitudetextfield, accuracytextfield;
- -(Void) Viewdidload {
-
- Lm = [[cllocationmanager alloc] init];
-
- If([Lm locationservicesenabled]) {
-
- Lm. Delegate = self;
-
- Lm. desiredaccuracy = kcllocationaccuracybest;
-
- Lm. distancefilter =1000. 0f;
-
- [Lm startupdatinglocation];
- }
-
- }
-
-
- -(Void) Locationmanager: (cllocationmanager *) Manager
-
- Didupdatetolocation: (cllocation *) newlocation
-
- Fromlocation: (cllocation *) oldlocation {
-
- Nsstring * lat = [[nsstring alloc] initwithformat :@"% G",
-
- Newlocation. Coordinate. Latitude];
- Latitudetextfield. Text = Lat;
-
-
- Nsstring * lng = [[nsstring alloc] initwithformat :@"% G",
-
- Newlocation. Coordinate. longpolling];
-
- Longitudetextfield. Text = LNG;
-
-
- Nsstring * ACC = [[nsstring alloc] initwithformat :@"% G",
- Newlocation. horizontalaccuracy];
-
- Accuracytextfield. Text = ACC;
-
-
- [ACC release];
-
- [Lat release];
-
- [LNG release];
-
- }
-
- -(Void) Locationmanager: (cllocationmanager *) Manager
-
- Didfailwitherror: (nserror *) error {
- Nsstring * MSG = [nsstring alloc]
-
- Initwithstring :@"Error obtaining location"];
-
- Uialertview * Alert = [[uialertview alloc]
-
- Initwithtitle :@"Error"
-
- Message: msg
- Delegate: Nil
-
- Cancelbuttontitle :@"Done"
-
- Otherbuttontitles: Nil];
-
- [Alert show];
-
- [MSG release];
- [Alert release];
-
- }
-
- -(Void) Dealloc {
-
- [Lm release];
-
- [Latitudetextfield release];
-
- [Longitudetextfield release];
-
- [Accuracytextfield release];
-
- [SuperDealloc];
-
- }
The previous Code creates an instance of the cllocationmanager class. Before using an object, you should check whether the user has enabled the device location service. If yes, you can use the desiredaccuracy attribute to specify the expected precision and use the following constant to specify the expected precision:
L kcllocationaccuracybest
L kcllocationaccuracynearesttenmeters
L kcllocationaccuracyhundredmeters
L kcllocationaccuracykilometer
L kcllocationaccuracythreekilometers
The distancefilter attribute allows you to specify the distance and location information that the device must move to update. The unit of this attribute is meter. If you want to get all the moving notifications, you can use the kcldistancefilternone constant and start the Location Manager using the startupdatinglocation method.
To obtain location information, you must handle the following two events:
L locationmanager: didupdatetolocation: fromlocation:
L locationmanager: didfailwitherror:
When a new location value is obtained, the device triggers the locationmanager: didupdatetolocation: fromlocation: event. If the location manager cannot determine the location information, it triggers the locationmanager: didfailwitherror: event.
When the device can determine the location, you may want to display the longitude and latitude values and precision. In this case, you can use the cllocation object. Its horizontalaccuracy attribute can specify the precision range, in meters.
Run the command-R command to test the program on the iPhone simulator. Figure 2 shows the latitude and longitude values of the simulator and the accuracy.
Figure 2 positioning test: These fixed values are always displayed when you test the sample program on the iPhone Simulator
Show Map
It will be even more interesting to locate the location coordinates on the map. Fortunately, the iPhone 3.0 SDK includes the map kit API, which allows you to display Google map in your program, the following is an example.
Or use the project created earlier to add a button in the visual window in the lbsviewcontroller. XIB file, as shown in figure 3.
Figure 3 view map button: After adding a button
Right-click the Framework Group in xcode and add a new framework mapkit. framework. Add the bold part in the following code in the lbsviewcontroller. h file:
Java code
-
- #Import<Uikit/uikit. h>
-
- #Import<Corelocation/corelocation. h>
-
- #Import<Mapkit/mapkit. h>
- @ InterfaceLbsviewcontroller: uiviewcontroller
-
- <Cllocationmanagerdelegate> {
-
- Iboutlet uitextfield * accuracytextfield;
-
- Iboutlet uitextfield * latitudetextfield;
-
- Iboutlet uitextfield * longitudetextfield;
-
- Cllocationmanager * lm;
-
-
- Mkmapview * mapview;
-
- }
- @ Property(Retain, nonatomic) uitextfield * accuracytextfield;
-
- @ Property(Retain, nonatomic) uitextfield * latitudetextfield;
-
- @ Property(Retain, nonatomic) uitextfield * longitudetextfield;
-
-
- -(Ibaction) btnviewmap: (ID) sender;
-
-
- @ End
Return to the interface editor, drag the button to the file owner project, and select btnviewmap :.
In the lbsviewcontroller. M file, add the bold Section in the following code:
Java code
-
- -(Ibaction) btnviewmap: (ID) sender {
-
- [Self. View addsubview: mapview];
-
- }
-
- -(Void) Viewdidload {
-
- Lm = [[cllocationmanager alloc] init];
-
- Lm. Delegate = self;
-
- Lm. desiredaccuracy = kcllocationaccuracybest;
-
- Lm. distancefilter =1000. 0f;
- [Lm startupdatinglocation];
-
-
- Mapview = [[mkmapview alloc] initwithframe: Self. View. bounds];
-
- Mapview. maptype = mkmaptypehybrid;
-
- }
-
- -(Void) Locationmanager: (cllocationmanager *) Manager
-
- Didupdatetolocation: (cllocation *) newlocation
-
- Fromlocation: (cllocation *) oldlocation {
- Nsstring * lat = [[nsstring alloc] initwithformat :@"% G",
-
- Newlocation. Coordinate. Latitude];
-
- Latitudetextfield. Text = Lat;
-
-
- Nsstring * lng = [[nsstring alloc] initwithformat :@"% G",
-
- Newlocation. Coordinate. longpolling];
-
- Longitudetextfield. Text = LNG;
-
- Nsstring * ACC = [[nsstring alloc] initwithformat :@"% G",
-
- Newlocation. horizontalaccuracy];
-
- Accuracytextfield. Text = ACC;
-
-
- [ACC release];
-
- [Lat release];
-
- [LNG release];
-
- Mkcoordinatespan span;
-
- Span. latitudedelta =.005;
-
- Span. longitudedelta =.005;
-
-
- Mkcoordinateregion region;
-
- Region. Center = newlocation. Coordinate;
-
- Region. span = span;
-
-
- [Mapview setregion: Region animated: True];
-
- }
- -(Void) Dealloc {
-
- [Mapview release];
-
- [Lm release];
-
- [Latitudetextfield release];
-
- [Longitudetextfield release];
-
- [Accuracytextfield release];
-
- [SuperDealloc];
-
- }
Code explanation:
L when a view is loaded, create an instance of the mkmapview class and set the displayed map type.
L when you click the view Map Button, add a mapview object to the current view.
L when the location information is updated, use the setregion: Method of the mapview object to enlarge the map.
In the iPhone simulator, press command-R to test the program. Click view map to display a map containing the location returned by the location manager. 4.
Figure 4 map location: display the map location by locating the kernel framework
Because the simulator always displays the same location. If you have an iPhone, you can actually feel it. When you move the location, you will see that the map is automatically updated. Set the distancefilter attribute to a smaller value to enhance the tracking experience.
Http://ming-fanglin.javaeye.com/blog/703744