Android學習十一:高德地圖使用

來源:互聯網
上載者:User

標籤:

寫這篇文章主要有三個目的:

  1.使用高德地圖api定位

  2.擷取天氣資料

  3.編程練手

檔案結構

 

資訊清單檔資訊說明:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="org.tonny"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="21" />    <uses-permission android:name="android.permission.INTERNET" />    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />    <uses-permission android:name="android.permission.READ_PHONE_STATE" />    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />    <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />    <uses-permission android:name="android.permission.WRITE_SETTINGS" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <meta-data            android:name="com.amap.api.v2.apikey"            android:value="****" />        <activity            android:name=".MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity            android:name=".WeatherActivity"            android:label="@string/title_activity_weather" >        </activity>    </application></manifest>

 

 

定位代碼:

package org.tonny;import com.amap.api.location.AMapLocation;import com.amap.api.location.AMapLocationListener;import com.amap.api.location.LocationManagerProxy;import com.amap.api.location.LocationProviderProxy;import android.app.Activity;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.content.Intent;import android.location.Location;import android.os.Bundle;import android.util.Log;import android.view.View;public class MainActivity extends Activity implements AMapLocationListener{    LocationManagerProxy mLocationManagerProxy;    @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mLocationManagerProxy = LocationManagerProxy.getInstance(MainActivity.this);        /**         * 第一個參數,使用定位的類型,混合模型,如網路,gps等 第二個參數,定位周期 第三個參數,移動多少距離的時候生效(只有GPS模式下有效)         */        mLocationManagerProxy.requestLocationData(LocationProviderProxy.AMapNetwork, 2 * 1000, 15, MainActivity.this);    }    public void onClick(View v)    {        Intent intent = new Intent(MainActivity.this, WeatherActivity.class);        startActivity(intent);    }    @Override    public void onLocationChanged(Location location)    {        // TODO Auto-generated method stub    }    @Override    public void onStatusChanged(String provider, int status, Bundle extras)    {        // TODO Auto-generated method stub    }    @Override    protected void onDestroy()    {        super.onDestroy();        mLocationManagerProxy.destroy();    }    @Override    public void onProviderEnabled(String provider)    {        // TODO Auto-generated method stub    }    @Override    public void onProviderDisabled(String provider)    {        // TODO Auto-generated method stub    }    @Override    public void onLocationChanged(AMapLocation location)    {        if (location != null && location.getAMapException().getErrorCode() == 0)        {            Log.e("Hello", location.toString());        }    }}

 

相應的布局檔案:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="${relativePackage}.${activityClass}" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />        <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="查看天氣"         android:onClick="onClick"/></RelativeLayout>

 

 

天氣代碼:

package org.tonny;import com.amap.api.location.AMapLocalWeatherForecast;import com.amap.api.location.AMapLocalWeatherListener;import com.amap.api.location.AMapLocalWeatherLive;import com.amap.api.location.LocationManagerProxy;import android.app.Activity;import android.os.Bundle;import android.util.Log;public class WeatherActivity extends Activity implements AMapLocalWeatherListener{    private LocationManagerProxy mLocationManagerProxy;    @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_weather);        mLocationManagerProxy = LocationManagerProxy.getInstance(WeatherActivity.this);        mLocationManagerProxy.requestWeatherUpdates(LocationManagerProxy.WEATHER_TYPE_LIVE, WeatherActivity.this);    }    @Override    protected void onDestroy()    {    }    @Override    public void onWeatherForecaseSearched(AMapLocalWeatherForecast forecast)    {    }    /*     * (non-Javadoc)     *      * @see     * com.amap.api.location.AMapLocalWeatherListener#onWeatherLiveSearched(     * com.amap.api.location.AMapLocalWeatherLive)     */    @Override    public void onWeatherLiveSearched(AMapLocalWeatherLive live)    {        Log.e("Weather", live.getCityCode());        Log.e("Weather", live.getCity());        Log.e("Weather", live.getTemperature());        Log.e("Weather", live.getWindDir());    }}

 

相應的布局檔案:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="${relativePackage}.${activityClass}" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="查看天氣" /></RelativeLayout>

 

Android學習十一:高德地圖使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.