获取GPS定位信息,并通过百度地图显示位置

flash,flex进行安卓或ios移动开发的时候,使用flash.sensors.Geolocation类,可以利用设备地理位置传感器获取GPS信息,可以获取的具体信息如下:
1,经度
2,纬度
3,高度
4,水平精度(米)
5,垂直精度(米)
6,速度
7,时间

 下面是一个GPS定位系统的demo,可以定时获取位置信息,同时使用百度地图的API实时的在地图上显示位置。

 效果图如下:

获取GPS定位信息,并通过百度地图显示位置_第1张图片
原文:Flex手机项目 - 获取gps定位信息,并通过百度地图显示位置

 

 

代码如下:

<?xml version="1.0" encoding="utf-8"?>

<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"

                 xmlns:s="library://ns.adobe.com/flex/spark"

                 creationComplete="onCreationComplete()"

                 title="GPS定位系统">

    <fx:Script>

        <![CDATA[

            import flash.sensors.Geolocation;

            

            private var geolocation:Geolocation;

            

            private function onCreationComplete():void

            {    

                //判断是否支持gps定位

                if(Geolocation.isSupported)

                {

                    geolocation = new Geolocation();

                    geolocation.setRequestedUpdateInterval(5000); //更新时间间隔

                    geolocation.addEventListener(GeolocationEvent.UPDATE, onUpdate);

                    geolocationTxt.text = "gps链接成功!";

                    if(!geolocation.muted){

                        trace("无法使用gps!");

                    }

                }

            }     

            

            private function onUpdate(e:GeolocationEvent):void

            {

                geolocationTxt.text = "经度: " + e.longitude.toString()  + "\n"

                                    + "纬度: " + e.latitude + "\n"

                                    + "高度: " + e.altitude + "\n"

                                    + "水平精度(米): " + e.horizontalAccuracy + "\n"

                                    + "垂直精度(米): " + e.verticalAccuracy + "\n"

                                    + "速度: " + e.speed + "\n"

                                    + "时间: " + e.timestamp;

                

                baiduImage.source = "http://api.map.baidu.com/staticimage?center="

                                   + e.longitude + ","

                                   + e.latitude + "&markers="

                                   + e.longitude + ","

                                   + e.latitude

                                   + "&zoom=14&width=420&height=300";

            }

        ]]>

    </fx:Script>

    

    <s:layout>

        <s:VerticalLayout paddingLeft="20" paddingRight="20" paddingBottom="20" paddingTop="20" />

    </s:layout>

    

    <s:Label text="定位数据:" />

    <s:Group width="100%">

        <s:Rect  id="bg" width="100%" height="100%">

            <s:fill><s:SolidColor color="0xffffff"/></s:fill>

            <s:stroke><s:SolidColorStroke color="0xc0c0c0" weight="2"/></s:stroke>

        </s:Rect>

        <s:Label id="geolocationTxt"

                 paddingLeft="10" paddingBottom="10" paddingTop="10" paddingRight="10"/>

    </s:Group>

    

    <s:Label text="百度地图:" paddingTop="10"/>

    <s:Image id="baiduImage" />

</s:View>

 

你可能感兴趣的:(获取GPS定位信息,并通过百度地图显示位置)