zoukankan      html  css  js  c++  java
  • 使用 Map API

    简单的显示Google地图

    添加MapKit.Framework框架

     HomeViewController.h code:

     
    #import <UIKit/UIKit.h>
    #import <MapKit/MapKit.h>
     
    @interface HomeViewController : UIViewController{
     
        MKMapView *map;
    }
     
    @end
     

    HomeViewController.m code:

    #import "HomeViewController.h"

     
    @interface HomeViewController ()
     
    @end
     
    @implementation HomeViewController
     
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        map = [[MKMapView alloc] initWithFrame:[self.view bounds]];
        [self.view addSubview:map];
        
    }
     
    - (void)dealloc{
     
        [map release];
        [super dealloc];
    }
     
    @end

    锁定位置 

    添加CoreLocation.Framework

      HomeViewController.h code:

     
    #import <UIKit/UIKit.h>
    #import <MapKit/MapKit.h>

    #import <CoreLocation/CoreLocation.h> 

     
    @interface HomeViewController : UIViewController{
     
        MKMapView *map;
    }
     
    @end
     

    HomeViewController.m code:

     #import "HomeViewController.h"

     
    @interface HomeViewController ()
     
    @end
     
    @implementation HomeViewController
     
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        map = [[MKMapView alloc] initWithFrame:[self.view bounds]];
        
        //设置地图显示模式
        //map.mapType = mkmaptype;//设置地图为卫星显示模式
        //map.mapType = MKMapTypeStandard;//显示一个标准的地图,包括街道和公路。它是一个默认值。
        map.mapType = MKMapTypeHybrid;//显示一个混合前两种情况的视图,也就是说在卫星视图上包含公路和街道的文字信息
        
        //设置地图显示当前设备所在的地理位置
        map.showsUserLocation = YES;//显示用户的位置
        
        CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(32.97386, 112.54938);
        
        float zoomLevel = 0.002;
        MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel, zoomLevel));//设置位置和范围
        [map setRegion:[map regionThatFits:region] animated:YES];
        
        [self.view addSubview:map];
        
    }
     
    - (void)dealloc{
     
        [map release];
        [super dealloc];
    }
     
    @end

     

    地图上的路径 

     HomeViewController.m code:

    #import "HomeViewController.h"
     
    @interface HomeViewController ()
     
    @end
     
    @implementation HomeViewController
     
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        map = [[MKMapView alloc] initWithFrame:[self.view bounds]];
        
        //设置地图显示模式
        map.mapType = MKMapTypeSatellite;//设置地图为卫星显示模式
        //map.mapType = MKMapTypeStandard;//显示一个标准的地图,包括街道和公路。它是一个默认值。
        //map.mapType = MKMapTypeHybrid;//显示一个混合前两种情况的视图,也就是说在卫星视图上包含公路和街道的文字信息
        
        //设置地图显示当前设备所在的地理位置
        map.showsUserLocation = YES;//显示用户的位置
        
        CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(39.992616, 116.389632);
        
        float zoomLevel = 0.002;
        MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel, zoomLevel));//设置位置和范围
        [map setRegion:[map regionThatFits:region] animated:YES];
        
        [self.view addSubview:map];
        
        map.delegate = self;
        [self createRoute];
        
    }
     
    //定义5个点
    - (void)createRoute{
     
        CLLocationCoordinate2D routeCoords[5]; 
        routeCoords[0] = CLLocationCoordinate2DMake(39.849227, 116.4083);
        routeCoords[1] = CLLocationCoordinate2DMake(39.949227, 116.395555);
        routeCoords[2] = CLLocationCoordinate2DMake(39.949227, 116.393623);
        routeCoords[3] = CLLocationCoordinate2DMake(39.949227, 116.401091);
        routeCoords[4] = CLLocationCoordinate2DMake(39.949227, 116.400833);
        
        MKPolyline *routeLine = [MKPolyline polylineWithCoordinates:routeCoords count:5];
        [map addOverlay:routeLine];
        [routeLine release];
    }
     
    //协议中的方法,
    -(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay{
     
        MKPolylineView *polyView = [[MKPolylineView alloc] initWithOverlay:overlay];
        polyView.strokeColor = [UIColor redColor];
        polyView.lineWidth = 5.0;
        
        return [polyView autorelease];
    }
     
    - (void)dealloc{
     
        [map release];
        [super dealloc];
    }
     
    @end

    添加标记“大头针” 

     添加一个NSObject类,命名为CustomAnnotation

     CustomAnnotation.h code

    #import <Foundation/Foundation.h>
    #import <MapKit/MapKit.h>
     
    @interface CustomAnnotation : NSObject
    <MKAnnotation>{
        
    }
     
    @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;//显示大头针的坐标
    @property (nonatomic, retain) NSString *title;

    @property (nonatomic, retain) NSString *subtitle; 

    @end 

    CustomAnnotation.m code

    #import "CustomAnnotation.h"
     
    @implementation CustomAnnotation
     
    @synthesize coordinate, title, subtitle;
     
    - (id)initWithCoordinate:(CLLocationCoordinate2D) coords{
     
        if (self = [super init]) {
            coordinate = coords;
        }
        return self;
    }
     
    - (void)dealloc{
     
        [title release];
        [subtitle release];
        [super dealloc];
    }
     
    @end
     

     修改HomeViewController.m代码

    #import "HomeViewController.h"
    #import "CustomAnnotation.h"
     
    @interface HomeViewController ()
     
    @end
     
    @implementation HomeViewController
     
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        map = [[MKMapView alloc] initWithFrame:[self.view bounds]];
        
        //设置地图显示模式
        map.mapType = MKMapTypeSatellite;//设置地图为卫星显示模式
        //map.mapType = MKMapTypeStandard;//显示一个标准的地图,包括街道和公路。它是一个默认值。
        //map.mapType = MKMapTypeHybrid;//显示一个混合前两种情况的视图,也就是说在卫星视图上包含公路和街道的文字信息
        
        //设置地图显示当前设备所在的地理位置
        map.showsUserLocation = YES;//显示用户的位置
        
        CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(39.992616, 116.389632);
        
        float zoomLevel = 0.002;
        MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel, zoomLevel));//设置位置和范围
        [map setRegion:[map regionThatFits:region] animated:YES];
        
        [self.view addSubview:map];
        
        map.delegate = self;
        //[self createRoute];
        [self createAnnotationWithCoords:coords];
        
    }
     
    //定义5个点
    - (void)createRoute{
     
        CLLocationCoordinate2D routeCoords[5]; 
        routeCoords[0] = CLLocationCoordinate2DMake(39.849227, 116.4083);
        routeCoords[1] = CLLocationCoordinate2DMake(39.949227, 116.395555);
        routeCoords[2] = CLLocationCoordinate2DMake(39.949227, 116.393623);
        routeCoords[3] = CLLocationCoordinate2DMake(39.949227, 116.401091);
        routeCoords[4] = CLLocationCoordinate2DMake(39.949227, 116.400833);
        
        MKPolyline *routeLine = [MKPolyline polylineWithCoordinates:routeCoords count:5];
        [map addOverlay:routeLine];
        [routeLine release];
    }
     
    //协议中的方法,
    -(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay{
     
        MKPolylineView *polyView = [[MKPolylineView alloc] initWithOverlay:overlay];
        polyView.strokeColor = [UIColor redColor];
        polyView.lineWidth = 5.0;
        
        return [polyView autorelease];
    }
     
    - (void)createAnnotationWithCoords:(CLLocationCoordinate2D) coords{
     
        CustomAnnotation *annotation = [[CustomAnnotation alloc] initWithCoordinate:coords];
        annotation.title = @"标题";
        annotation.subtitle = @"子标题";
        [map addAnnotation:annotation];
        [annotation release];
    }
     
    - (void)dealloc{
     
        [map release];
        [super dealloc];
    }
     

    @end 

     

  • 相关阅读:
    Azure PowerShell (2) 修改Azure订阅名称
    Windows Azure Platform Introduction (11) 了解Org ID、Windows Azure订阅、账户
    Azure PowerShell (3) 上传证书
    Azure PowerShell (1) PowerShell入门
    Windows Azure Service Bus (2) 队列(Queue)入门
    Windows Azure Service Bus (1) 基础
    Windows Azure Cloud Service (10) Role的生命周期
    Windows Azure Cloud Service (36) 在Azure Cloud Service配置SSL证书
    Android studio 使用心得(一)—android studio快速掌握快捷键
    android 签名、混淆打包
  • 原文地址:https://www.cnblogs.com/hanjun/p/2747901.html
Copyright © 2011-2022 走看看