這兩天主要就去做百度地圖去了,剛開始一頭霧水,什麼遮蓋物什麼興趣點..... 所以這篇文章先來介紹些基礎知識吧,遮蓋物,就是遮蓋在地圖上的東西,比如說 興趣點,就是圓圈圈的地點,包括什麼餐飲大廈之類之類的 好,然後說說地圖,首先你一定要去搞個KEY,因為沒有這個東西,你地圖都載入不出來...我就偷懶 ...
這兩天主要就去做百度地圖去了,剛開始一頭霧水,什麼遮蓋物什麼興趣點.....
所以這篇文章先來介紹些基礎知識吧,遮蓋物,就是遮蓋在地圖上的東西,比如說
興趣點,就是圓圈圈的地點,包括什麼餐飲大廈之類之類的
好,然後說說地圖,首先你一定要去搞個KEY,因為沒有這個東西,你地圖都載入不出來...我就偷懶了,繞了個大圈。然後申請項目,也看官方
接著你就可以下個官方的Demo,然後看著API自己改代碼了。demo:http://lbsyun.baidu.com/sdk/download?qq-pf-to=pcqq.c2c
好,貼代碼,這個是輸入經緯度,然後定一個位置,可以點擊附件的地方。然後獲取點擊的經緯度和地點
.java
package com.example.mybaidumap;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnMapClickListener;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeOption;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.baidu.nplatform.comapi.basestruct.GeoPoint;
import com.baidu.mapapi.map.LocationData;
import com.baidu.mapapi.map.MapController;
//百度地圖,需要參數:經緯度.可點擊,結果獲取為:地址,經緯度
public class MainActivity extends Activity implements
OnGetGeoCoderResultListener,OnClickListener {
MapView mMapView = null;
BaiduMap mBaiduMap;
boolean isFirstLoc = true;
private String address;
GeoCoder mSearch;
private TextView conaddress;
private MapController mMapController = null;
LatLng latlng;
private Button ok;
double lon,lat;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
//初始化,lat,lon為經緯度值
ok=(Button)findViewById(R.id.ok);
ok.setOnClickListener(this);
conaddress=(TextView) findViewById(R.id.conaddress);
lat=39.904965;
lon=116.327764;
latlng=new LatLng(lat,lon);
// 獲取地圖控制項引用
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
mSearch = GeoCoder.newInstance();
// 設置地圖初始Zoom大小
mBaiduMap.setMapStatus(MapStatusUpdateFactory.zoomTo(17.0f));
// 開啟定點陣圖層
//mBaiduMap.setMyLocationEnabled(true);
// 初始化搜索模塊,註冊事件監聽
mSearch = GeoCoder.newInstance();
mSearch.setOnGetGeoCodeResultListener(this);
mSearch.reverseGeoCode(new ReverseGeoCodeOption()
.location(latlng));
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);// 打開gps
option.setCoorType("bd09ll"); // 設置坐標類型
option.setScanSpan(1000);
option.setAddrType("all");
OnMapClickListener listener = new OnMapClickListener() {
/**
* 地圖單擊事件回調函數
*
* @param point,poi
* 點擊的地理坐標,點擊的興趣點
*/
@Override
public void onMapClick(LatLng point) {
// TODO Auto-generated method stub
LatLng ptCenter =point;
// 反Geo搜索
mSearch.reverseGeoCode(new ReverseGeoCodeOption()
.location(ptCenter));
popWin(point);
lon=point.longitude;
lat=point.latitude;
}
@Override
public boolean onMapPoiClick(MapPoi poi) {
// TODO Auto-generated method stub
conaddress.setText(poi.getName());
Log.w("tag",poi.getName());
popWin(poi.getPosition());
lat=poi.getPosition().latitude;
lon=poi.getPosition().longitude;
return false;
}
};
mBaiduMap.setOnMapClickListener(listener);
}
@Override
protected void onDestroy() {
super.onDestroy();
// 在activity執行onDestroy時執行mMapView.onDestroy(),實現地圖生命周期管理
mMapView.onDestroy();
mSearch.destroy();
}
@Override
protected void onResume() {
super.onResume();
// 在activity執行onResume時執行mMapView. onResume (),實現地圖生命周期管理
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
// 在activity執行onPause時執行mMapView. onPause (),實現地圖生命周期管理
mMapView.onPause();
}
// 自定義提示圖標market
public void popWin(LatLng point) {
// 構建Marker圖標
BitmapDescriptor bitmap = BitmapDescriptorFactory
.fromResource(R.drawable.nav_turn_via_1);
// 構建MarkerOption,用於在地圖上添加Marker
OverlayOptions option = new MarkerOptions().position(point)
.icon(bitmap);
// 在地圖上添加Marker,並顯示
mBaiduMap.clear();
mBaiduMap.addOverlay(option);
}
//地址編碼
@Override
public void onGetGeoCodeResult(GeoCodeResult result) {
if (result == null || result.error !=
SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(MainActivity.this, "抱歉,未能找到結果",
Toast.LENGTH_LONG)
.show();
return;
}
mBaiduMap.clear();
mBaiduMap.addOverlay(new MarkerOptions().position
(result.getLocation())
.icon(BitmapDescriptorFactory
.fromResource
(R.drawable.nav_turn_via_1)));
mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(result
.getLocation()));
String strInfo = String.format("緯度:%f 經度:%f",
result.getLocation().latitude,
result.getLocation().longitude);
Toast.makeText(MainActivity.this, strInfo,
Toast.LENGTH_LONG).show();
conaddress.setText(result.getAddress());
}
//正編
@Override
public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
if (result == null || result.error !=
SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(MainActivity.this, "抱歉,未能找到結果",
Toast.LENGTH_LONG)
.show();
return;
}
mBaiduMap.clear();
mBaiduMap.addOverlay(new MarkerOptions().position
(result.getLocation())
.icon(BitmapDescriptorFactory
.fromResource
(R.drawable.nav_turn_via_1)));
mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(result
.getLocation()));
conaddress.setText(result.getAddress());
}
//點擊確定傳值
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id=v.getId();
if(id==R.id.ok)
{
if(conaddress.getText()!=null ){
Intent intent =new Intent(MainActivity.this,Resultt.class);
intent.putExtra("address", conaddress.getText());
intent.putExtra("lat",lat+"");
intent.putExtra("lon",lon+"");
startActivity(intent);
}
}
}
}
。xml:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
<TextView
android:id="@+id/conaddress"
android:layout_width="fill_parent"
android:gravity="left|center_vertical"
android:paddingLeft="10dp"
android:layout_height="40dp"
android:textSize="13sp"
android:background="#45000000"
android:layout_alignParentTop="true"
android:textColor="#FFFFFF"
/>
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignBottom="@+id/conaddress"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:textSize="20dip"
android:text="確定" />
</RelativeLayout>
</FrameLayout>
</RelativeLayout>
源碼下載地址:http://www.jinhusns.com/Products/Download