Iphone 需要获取当前位置和目标位置pin
如何使用绿色pin获取当前位置,使用红色pin获取目标位置 当我处理一些东西时,我只得到带有红色pin的目标位置,而不是当前位置 我的源代码Iphone 需要获取当前位置和目标位置pin,iphone,Iphone,如何使用绿色pin获取当前位置,使用红色pin获取目标位置 当我处理一些东西时,我只得到带有红色pin的目标位置,而不是当前位置 我的源代码 #import <UIKit/UIKit.h> #import <MapKit/MapKit.h> #import <MapKit/MKAnnotation.h> #import <CoreLocation/CoreLocation.h> @interface AddressAnnotation : NSO
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <MapKit/MKAnnotation.h>
#import <CoreLocation/CoreLocation.h>
@interface AddressAnnotation : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *mTitle;
NSString *mSubTitle;
// CLLocationManager *locationManager;
// CLLocation *currentLocation;
}
@end
@interface MapViewController : UIViewController <CLLocationManagerDelegate,MKMapViewDelegate> {
IBOutlet MKMapView *mapView;
AddressAnnotation *addAnnotation;
NSString *address;
CLLocationManager *locationManager;
CLLocation *currentLocation;
}
+(MapViewController *)sharedInstance;
-(void)start;
-(void)stop;
-(BOOL)locationKnown;
@property(nonatomic,retain)CLLocation *currentLocation;
@property(nonatomic,retain)NSString *address;
-(CLLocationCoordinate2D) addressLocation;
-(void)showAddress;
@end
#import "MapViewController.h"
@implementation AddressAnnotation
@synthesize coordinate;
//@synthesize currentLocation;
- (NSString *)subtitle{
//return @"Sub Title";
return @"Event";
}
- (NSString *)title{
//return @"Title";
return @"Allure-Exclusive";
}
-(id)initWithCoordinate:(CLLocationCoordinate2D) c{
coordinate=c;
//NSLog(@"%f,%f",c.latitude,c.longitude);
return self;
}
@end
@implementation MapViewController
@synthesize address;
@synthesize currentLocation;
static MapViewController *sharedInstance;
+(MapViewController *)sharedInstance{
@synchronized (self)
{
if (!sharedInstance)
[[MapViewController alloc]init];
}
return sharedInstance;
}
+(id)alloc{
@synchronized(self){
NSAssert(sharedInstance==nil,"Attempted to allocate a second instance of a singleton LocationController.");
sharedInstance = [super alloc];
}
return sharedInstance;
}
-(id)init{
if(self==[super init]){
self.currentLocation=[[CLLocation alloc]init];
locationManager=[[CLLocationManager alloc]init];
locationManager.delegate=self;
[self start];
}
return self;
}
-(void)start{
NSLog(@"Start");
mapView.showsUserLocation=YES;
[locationManager startUpdatingLocation];
}
-(void)stop{
mapView.showsUserLocation=NO;
[locationManager stopUpdatingLocation];
}
-(BOOL)locationKnown{
if (round(currentLocation.speed)==-1)
return NO;
else return YES;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if (abs([newLocation.timestamp timeIntervalSinceDate:[NSDate date]])<120){
self.currentLocation=newLocation;
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
UIAlertView *alert;
alert=[[UIAlertView alloc]initWithTitle:@"Error" message:[error description] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.title=@"Map-View";
// [self addressLocation];
[self showAddress];
NSLog(@"address is %@",address);
}
-(void)showAddress{
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.5;
span.longitudeDelta=0.5;
CLLocationCoordinate2D location = [self addressLocation];
region.span=span;
region.center=location;
if(addAnnotation != nil) {
[mapView removeAnnotation:addAnnotation];
[addAnnotation release];
addAnnotation = nil;
}
addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:location];
[mapView addAnnotation:addAnnotation];
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
}
-(CLLocationCoordinate2D) addressLocation {
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv",
[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
NSLog(@"locationString %@",locationString);
NSArray *listItems = [locationString componentsSeparatedByString:@","];
double latitude = 0.0;
double longitude = 0.0;
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
NSLog(@"listItems %@",[listItems objectAtIndex:2]);
}
else {
//Show error
}
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
return location;
}
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
if (annotation==mapView.userLocation) {
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
annView.pinColor = MKPinAnnotationColorGreen;
annView.animatesDrop=YES;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
return annView;
//
}
else {
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
annView.pinColor = MKPinAnnotationColorRed;
annView.animatesDrop=YES;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
return annView;
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// [self stop];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[address release];
[super dealloc];
}
@end
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
if (annotation==mapView.userLocation)
{
mapView.userLocation.title=@"Current Location";
[mapView setRegion:MKCoordinateRegionMakeWithDistance(mapView.userLocation.coordinate, 1000, 1000)animated:YES];
return nil;
}
else {
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
annView.pinColor = MKPinAnnotationColorRed;
annView.animatesDrop=YES;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
return annView;
}
}
#导入
#进口
#进口
#进口
@接口地址注释:NSObject{
CLLocationCoordinate2D坐标;
NSString*mTitle;
NSString*mSubTitle;
//CLLocationManager*locationManager;
//CLLocation*当前位置;
}
@结束
@接口MapViewController:UIViewController{
IBMKMAPVIEW*mapView;
AddressAnnotation*addAnnotation;
NSString*地址;
CLLocationManager*locationManager;
CLLocation*当前位置;
}
+(MapViewController*)共享状态;
-(无效)开始;
-(无效)停止;
-(BOOL)位置已知;
@属性(非原子,保留)CLLocation*currentLocation;
@属性(非原子,保留)NSString*地址;
-(CLLocationCoordinate2D)地址位置;
-(无效)地址;
@结束
#导入“MapViewController.h”
@实现地址注释
@综合坐标;
//@综合当前位置;
-(NSString*)副标题{
//返回@“子标题”;
返回@“事件”;
}
-(NSString*)标题{
//返回@“标题”;
返回@“诱惑专属”;
}
-(id)initWithCoordinate:(CLLocationCoordinate2D)c{
坐标=c;
//NSLog(@“%f,%f”,c.纬度,c.经度);
回归自我;
}
@结束
@MapViewController的实现
@综合地址;
@综合当前位置;
静态MapViewController*共享状态;
+(MapViewController*)共享状态{
@同步(自)
{
如果(!sharedInstance)
[[MapViewController alloc]init];
}
返回共享状态;
}
+(id)alloc{
@同步(自){
NSAssert(sharedInstance==nil,“试图分配singleton LocationController的第二个实例”);
sharedInstance=[super alloc];
}
返回共享状态;
}
-(id)init{
if(self==[super init]){
self.currentLocation=[[CLLocation alloc]init];
locationManager=[[CLLocationManager alloc]init];
locationManager.delegate=self;
[自启动];
}
回归自我;
}
-(无效)开始{
NSLog(@“开始”);
mapView.showsUserLocation=是;
[locationManager startUpdatingLocation];
}
-(无效)停止{
mapView.showsUserLocation=否;
[locationManager停止更新位置];
}
-(BOOL)位置已知{
if(圆形(当前位置速度)=-1)
返回否;
否则返回YES;
}
-(void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation{
如果(abs([newLocation.timestamp timeintervalncencedate:[NSDate date]])=4&[[listItems objectAtIndex:0]IsequalString:@“200”]){
纬度=[[listItems对象索引:2]双值];
经度=[[listItems对象索引:3]doubleValue];
NSLog(@“listItems%@,[listItems对象索引:2]);
}
否则{
//显示错误
}
CLLOCATION坐标2D定位;
位置。纬度=纬度;
location.longitude=经度;
返回位置;
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释{
if(注释==mapView.userLocation){
MKPinAnnotationView*annView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:@“currentloc”];
annView.pinColor=MKPinAnnotationColorGreen;
annView.animatesDrop=是;
annView.canShowCallout=是;
annView.calloutOffset=CGPointMake(-5,5);
返回视图;
//
}
否则{
MKPinAnnotationView*annView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:@“currentloc”];
annView.pinColor=MKPinAnnotationColorRed;
annView.animatesDrop=是;
annView.canShowCallout=是;
annView.calloutOffset=CGPointMake(-5,5);
返回视图;
}
}
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation{
//覆盖以允许任何方向。
返回YES;
}
-(无效)未收到记忆警告{
//如果视图没有superview,则释放该视图。
[超级记忆警告];
//释放所有未使用的缓存数据、图像等。
}
-(无效)视图卸载{
//[自动停止];
[超级视频下载];
//释放主视图的所有保留子视图。
//例如,self.myOutlet=nil;
}
-(无效)解除锁定{
[地址发布];
[super dealoc];
}
@结束
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释{
if(注释==mapView.userLocation)
{
mapView.userLocation.title=@“当前位置”;
[mapView设置区域:MKCoordinateRegionMakeWithDistance(mapView.userLocation.coordinate,10001000)已设置动画:是];
返回零;
}
否则{
MKPinAnnotationView*annView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:@“currentloc”];
annView.pinColor=MKPinAnnotationColorRed;
annView.animatesDrop=是;
annView.canShowCallout=是;
annView.calloutOffset=CGPointMake(-5,5);
返回视图;
}
}
当我改变方法的时候。它指向蓝色并闪烁,但它指向不同的位置,在无限环马里亚尼大街的位置
它是在模拟器中运行的。您需要设置showsUserLocation
mapView.showsUserLocation=YES代码>我在viewdidload mapview.showsuserlocation=yes中设置了它;但是它不显示,先生。