
基于高德地图SDK的天气查询
Android移动开发技术
2019年11月12日
摘要
本软件基于Android操作系统,以Android Studio为开发环境,通过XML和JAVA语言的编写,设计成为一款能够通过地图点击查询天气的app。
功能包括点击中国地图直接查询点击点的经纬度,获取该经纬度所在的地级市,并显示当前城市的天气情况,如当前温度,天气,风向,风强。
设计描述
- 系统的功能描述
本软件通过点击地图容器首先查询点击位置的经纬度,并加以Marker点标记,同时对经纬度进行逆地理编码,获取此经纬度所在的地级市,得到地级市地名后即可调用高德sdk对该城市进行天气查询,并显示与marker上。
- 需求分析
1.需要利用点击监听函数获取用户点击地点的经纬度。
2.按获取的经纬度利用逆地理编码查询反向找到该经纬度对应的城市名。
3.利用高德sdk的天气查询接口,对获取的城市名异步搜索,获取天气信息并保存。
4.将天气信息显示在Marker信息中。
- 模块设计
1.地图显示模块,利用高德地图SDK使用MapView容器将3D地图直接显示于主界面上,方便后续点击查询。
2.Marker点标记模块,捕捉用户点击地点,获取经纬度,并显示用户查询的城市名及天气信息。
- 注意点
当在高德平台发布app时,不能采用项目中的sha1值进行授权,否则查询访问请求将失败,应采用Grade中的sha1值。
功能模块代码:
1.地图显示,利用MapView容器显示3D地图
mMapView = (MapView) findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);
if(aMap==null)
{
aMap=mMapView.getMap();
}
2.地图点击事件,利用地图点击监听事件获取点击地点的经纬度,并标记Marker
aMap.setOnMapClickListener(this);
@Override
public void onMapClick(LatLng latLng) {
aMap.clear();
jingdu=latLng.latitude;
weidu=latLng.longitude;
Toast.makeText(this,"经度:"+jingdu+" 维度:"+weidu,Toast.LENGTH_SHORT).show();
markerOptions=new MarkerOptions();
markerOptions.position(latLng);
LatLonPoint l=new LatLonPoint(jingdu,weidu);
RegeocodeQuery query=new RegeocodeQuery(l,200,GeocodeSearch.AMAP);
geocodeSearch.getFromLocationAsyn(query);
markerOptions.title(city).snippet("发布时间:"+time+"\n"+"温度:"+temp+"℃ "+"天气:"+weather+" 湿度:"+shidu+"\n"+"风向:"+windd+" "+"风强:"+windp+”级;");
aMap.addMarker(markerOptions);
aMap.moveCamera(CameraUpdateFactory.changeLatLng(latLng));}
3.逆地理编码查询,利用地理编码查询监听函数查询对得到的地理编码进行逆查询,得到查询城市。
geocodeSearch=new GeocodeSearch(this);
geocodeSearch.setOnGeocodeSearchListener(this);
@Override
public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
city=regeocodeResult.getRegeocodeAddress().getCity();
mquery=new WeatherSearchQuery(city,WeatherSearchQuery.WEATHER_TYPE_LIVE);
weatherSearch.setQuery(mquery);
weatherSearch.searchWeatherAsyn();}
4.天气信息查询显示,利用天气查询监听函数对该城市天气进行查询并显示。
weatherSearch=new WeatherSearch(this);
weatherSearch.setOnWeatherSearchListener(this);
@Override
public void onWeatherLiveSearched(LocalWeatherLiveResult localWeatherLiveResult, int i) {
time=localWeatherLiveResult.getLiveResult().getReportTime();
temp=localWeatherLiveResult.getLiveResult().getTemperature();
weather=localWeatherLiveResult.getLiveResult().getWeather();
windd=localWeatherLiveResult.getLiveResult().getWindDirection();
windp=localWeatherLiveResult.getLiveResult().getWindPower();
shidu=localWeatherLiveResult.getLiveResult().getHumidity();
Toast.makeText(this,"查询成功",Toast.LENGTH_LONG).show();
}
软件测试:
- 打开地图

- 选择徐州市

- 获取经纬度

- 获取城市名查询天气

- 显示天气信息

源代码:
xml:
Xml布局代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.amap.api.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.weathermap">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="com.amap.api.v2.apikey" android:value="509daa93a6fcd8f42b3019a27399fdbd"> </meta-data>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
</manifest>
java:
package com.example.weathermap;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.TextOptions;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.amap.api.services.weather.LocalWeatherForecast;
import com.amap.api.services.weather.LocalWeatherForecastResult;
import com.amap.api.services.weather.LocalWeatherLiveResult;
import com.amap.api.services.weather.WeatherSearch;
import com.amap.api.services.weather.WeatherSearchQuery;
import java.util.List;
public class MainActivity extends AppCompatActivity implements AMap.OnMapClickListener, GeocodeSearch.OnGeocodeSearchListener, WeatherSearch.OnWeatherSearchListener {
private MapView mMapView=null;
double jingdu;
double weidu;
String city,time,temp,weather,windd,windp,shidu;
private AMap aMap=null;
private GeocodeSearch geocodeSearch;
private WeatherSearch weatherSearch;
private MarkerOptions markerOptions;
private WeatherSearchQuery mquery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMapView = (MapView) findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);
if(aMap==null)
{
aMap=mMapView.getMap();
}
geocodeSearch=new GeocodeSearch(this);
geocodeSearch.setOnGeocodeSearchListener(this);
weatherSearch=new WeatherSearch(this);
weatherSearch.setOnWeatherSearchListener(this);
aMap.setOnMapClickListener(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);
}
@Override
public void onMapClick(LatLng latLng) {
aMap.clear();
jingdu=latLng.latitude;
weidu=latLng.longitude;
Toast.makeText(this,"经度:"+jingdu+" 维度:"+weidu,Toast.LENGTH_SHORT).show();
markerOptions=new MarkerOptions();
markerOptions.position(latLng);
LatLonPoint l=new LatLonPoint(jingdu,weidu);
RegeocodeQuery query=new RegeocodeQuery(l,200,GeocodeSearch.AMAP);
geocodeSearch.getFromLocationAsyn(query);
markerOptions.title(city).snippet("发布时间:"+time+"\n"+"温度:"+temp+"℃ "+"天气:"+weather+" 湿度:"+shidu+"\n"+
"风向:"+windd+" "+"风强:"+windp+"级;");
aMap.addMarker(markerOptions);
aMap.moveCamera(CameraUpdateFactory.changeLatLng(latLng));
}
@Override
public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
city=regeocodeResult.getRegeocodeAddress().getCity();
mquery=new WeatherSearchQuery(city,WeatherSearchQuery.WEATHER_TYPE_LIVE);
weatherSearch.setQuery(mquery);
weatherSearch.searchWeatherAsyn();
}
@Override
public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
}
@Override
public void onWeatherLiveSearched(LocalWeatherLiveResult localWeatherLiveResult, int i) {
time=localWeatherLiveResult.getLiveResult().getReportTime();
temp=localWeatherLiveResult.getLiveResult().getTemperature();
weather=localWeatherLiveResult.getLiveResult().getWeather();
windd=localWeatherLiveResult.getLiveResult().getWindDirection();
windp=localWeatherLiveResult.getLiveResult().getWindPower();
shidu=localWeatherLiveResult.getLiveResult().getHumidity();
Toast.makeText(this,"查询成功",Toast.LENGTH_LONG).show();
}
@Override
public void onWeatherForecastSearched(LocalWeatherForecastResult localWeatherForecastResult, int i) {
}
}
