- public void getLocationProvider()
- {
- try
- {
- Criteria mCriteria01 = new Criteria();
- mCriteria01.setAccuracy(Criteria.ACCURACY_FINE);
- mCriteria01.setAltitudeRequired(false);
- mCriteria01.setBearingRequired(false);
- mCriteria01.setCostAllowed(true);
- mCriteria01.setPowerRequirement(Criteria.POWER_LOW);
- strLocationProvider =
- mLocationManager01.getBestProvider(mCriteria01, true);
-
- mLocation01 = mLocationManager01.getLastKnownLocation
- (strLocationProvider);
- }
- catch(Exception e)
- {
- mTextView01.setText(e.toString());
- e.printStackTrace();
- }
- }
- public void getLocationProvider()
- {
- try
- {
- Criteria mCriteria01 = new Criteria();
- mCriteria01.setAccuracy(Criteria.ACCURACY_FINE);
- mCriteria01.setAltitudeRequired(false);
- mCriteria01.setBearingRequired(false);
- mCriteria01.setCostAllowed(true);
- mCriteria01.setPowerRequirement(Criteria.POWER_LOW);
- strLocationProvider =
- mLocationManager01.getBestProvider(mCriteria01, true);
-
- mLocation01 = mLocationManager01.getLastKnownLocation
- (strLocationProvider);
- }
- catch(Exception e)
- {
- mTextView01.setText(e.toString());
- e.printStackTrace();
- }
- }
public void getLocationProvider(){try{Criteria mCriteria01 = new Criteria();mCriteria01.setAccuracy(Criteria.ACCURACY_FINE);mCriteria01.setAltitudeRequired(false);mCriteria01.setBearingRequired(false);mCriteria01.setCostAllowed(true);mCriteria01.setPowerRequirement(Criteria.POWER_LOW);strLocationProvider =mLocationManager01.getBestProvider(mCriteria01, true);mLocation01 = mLocationManager01.getLastKnownLocation(strLocationProvider);}catch(Exception e){mTextView01.setText(e.toString());e.printStackTrace();}}
獲取經(jīng)緯度,并返回GeoPoint對(duì)象 - private GeoPoint getGeoByLocation(Location location)
- {
- GeoPoint gp = null;
- try
- {
-
- if (location != null)
- {
- double geoLatitude = location.getLatitude()*1E6;
- double geoLongitude = location.getLongitude()*1E6;
- gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);
- }
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- return gp;
- }
- private GeoPoint getGeoByLocation(Location location)
- {
- GeoPoint gp = null;
- try
- {
-
- if (location != null)
- {
- double geoLatitude = location.getLatitude()*1E6;
- double geoLongitude = location.getLongitude()*1E6;
- gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);
- }
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- return gp;
- }
private GeoPoint getGeoByLocation(Location location){GeoPoint gp = null;try{/* 當(dāng)Location存在 */if (location != null){double geoLatitude = location.getLatitude()*1E6;double geoLongitude = location.getLongitude()*1E6;gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);}}catch(Exception e){e.printStackTrace();}return gp;}
將經(jīng)緯度轉(zhuǎn)換成實(shí)際屏幕坐標(biāo) - Point myScreenCoords = new Point();
- GeoPoint tmpGeoPoint = new GeoPoint((int)(mLocation.getLatitude()*1E6),(int)(mLocation.getLongitude()*1E6));
- mapView.getProjection().toPixels(tmpGeoPoint, myScreenCoords);
- Point myScreenCoords = new Point();
- GeoPoint tmpGeoPoint = new GeoPoint((int)(mLocation.getLatitude()*1E6),(int)(mLocation.getLongitude()*1E6));
- mapView.getProjection().toPixels(tmpGeoPoint, myScreenCoords);
Point myScreenCoords = new Point();GeoPoint tmpGeoPoint = new GeoPoint((int)(mLocation.getLatitude()*1E6),(int)(mLocation.getLongitude()*1E6));mapView.getProjection().toPixels(tmpGeoPoint, myScreenCoords);
點(diǎn)擊MapView任意一點(diǎn)獲得坐標(biāo) - @Override
- public boolean onTouchEvent(MotionEvent ev) {
- int actionType = ev.getAction();
- switch (actionType) {
- case MotionEvent.ACTION_UP:
- Projection proj = mapView.getProjection();
- GeoPoint loc = proj.fromPixels((int)arg0.getX(), (int)arg0.getY());
- String sirina=Double.toString(loc.getLongitudeE6()/1000000);
- String dolzina=Double.toString(loc.getLatitudeE6()/1000000);
-
- }
-
- return false;
- }
- @Override
- public boolean onTouchEvent(MotionEvent ev) {
- int actionType = ev.getAction();
- switch (actionType) {
- case MotionEvent.ACTION_UP:
- Projection proj = mapView.getProjection();
- GeoPoint loc = proj.fromPixels((int)arg0.getX(), (int)arg0.getY());
- String sirina=Double.toString(loc.getLongitudeE6()/1000000);
- String dolzina=Double.toString(loc.getLatitudeE6()/1000000);
-
- }
-
- return false;
- }
@Overridepublic boolean onTouchEvent(MotionEvent ev) {int actionType = ev.getAction();switch (actionType) {case MotionEvent.ACTION_UP:Projection proj = mapView.getProjection();GeoPoint loc = proj.fromPixels((int)arg0.getX(), (int)arg0.getY());String sirina=Double.toString(loc.getLongitudeE6()/1000000);String dolzina=Double.toString(loc.getLatitudeE6()/1000000);}return false;}
經(jīng)緯度改變來(lái)刷新地圖 - public void refreshMapView()
- {
- GeoPoint p = new GeoPoint((int)(dLat* 1E6), (int)(dLng* 1E6));
- mMapView01.displayZoomControls(true);
-
- mMapController01.animateTo(p);
- mMapController01.setZoom(intZoomLevel);
- }
- public void refreshMapView()
- {
- GeoPoint p = new GeoPoint((int)(dLat* 1E6), (int)(dLng* 1E6));
- mMapView01.displayZoomControls(true);
-
- mMapController01.animateTo(p);
- mMapController01.setZoom(intZoomLevel);
- }
public void refreshMapView(){GeoPoint p = new GeoPoint((int)(dLat* 1E6), (int)(dLng* 1E6));mMapView01.displayZoomControls(true);/* 將Map的中點(diǎn)移至GeoPoint */mMapController01.animateTo(p);mMapController01.setZoom(intZoomLevel);}
根據(jù)當(dāng)前的經(jīng)緯度,獲取相關(guān)的一些地址信息 -
-
-
- Geocoder gc = new Geocoder
- (EX09_05.this, Locale.getDefault());
-
-
- double geoLatitude = (int)gp.getLatitudeE6()/1E6;
- double geoLongitude = (int)gp.getLongitudeE6()/1E6;
-
-
- List<Address> lstAddress =
- gc.getFromLocation(geoLatitude, geoLongitude, 1);
-
- StringBuilder sb = new StringBuilder();
-
-
- if (lstAddress.size() > 0)
- {
- Address adsLocation = lstAddress.get(0);
-
- for(int i=0;i<adsLocation.getMaxAddressLineIndex();i++)
- {
- sb.append(adsLocation.getAddressLine(i)).append("\n");
- }
- sb.append(adsLocation.getLocality()).append("\n");
- sb.append(adsLocation.getPostalCode()).append("\n");
- sb.append(adsLocation.getCountryName());
- }
-
-
-
-
- Geocoder gc = new Geocoder
- (EX09_05.this, Locale.getDefault());
-
-
- double geoLatitude = (int)gp.getLatitudeE6()/1E6;
- double geoLongitude = (int)gp.getLongitudeE6()/1E6;
-
-
- List<Address> lstAddress =
- gc.getFromLocation(geoLatitude, geoLongitude, 1);
-
- StringBuilder sb = new StringBuilder();
-
-
- if (lstAddress.size() > 0)
- {
- Address adsLocation = lstAddress.get(0);
-
- for(int i=0;i<adsLocation.getMaxAddressLineIndex();i++)
- {
- sb.append(adsLocation.getAddressLine(i)).append("\n");
- }
- sb.append(adsLocation.getLocality()).append("\n");
- sb.append(adsLocation.getPostalCode()).append("\n");
- sb.append(adsLocation.getCountryName());
- }
-
/* 創(chuàng)建Geocoder對(duì)象 *///根據(jù)地理環(huán)境來(lái)確定編碼//注意這個(gè)Locale是java.util.Locale包的類(lèi),獲取當(dāng)前系統(tǒng)設(shè)定的語(yǔ)言Geocoder gc = new Geocoder(EX09_05.this, Locale.getDefault());/* 取出地理坐標(biāo)經(jīng)緯度 */double geoLatitude = (int)gp.getLatitudeE6()/1E6;double geoLongitude = (int)gp.getLongitudeE6()/1E6;/* 自經(jīng)緯度取得地址(可能有多行地址) */List<Address> lstAddress =gc.getFromLocation(geoLatitude, geoLongitude, 1);StringBuilder sb = new StringBuilder();/* 判斷地址是否為多行 */if (lstAddress.size() > 0){Address adsLocation = lstAddress.get(0);for(int i=0;i<adsLocation.getMaxAddressLineIndex();i++){sb.append(adsLocation.getAddressLine(i)).append("\n");}sb.append(adsLocation.getLocality()).append("\n");sb.append(adsLocation.getPostalCode()).append("\n");sb.append(adsLocation.getCountryName());}
根據(jù)輸入地址,取得其GeoPoint對(duì)象 - private GeoPoint getGeoByAddress(String strSearchAddress)
- {
- GeoPoint gp = null;
- try
- {
- if(strSearchAddress!="")
- {
- Geocoder mGeocoder01 = new Geocoder
- (EX09_07.this, Locale.getDefault());
-
- List<Address> lstAddress = mGeocoder01.getFromLocationName
- (strSearchAddress, 1);
- if (!lstAddress.isEmpty())
- {
- Address adsLocation = lstAddress.get(0);
- double geoLatitude = adsLocation.getLatitude()*1E6;
- double geoLongitude = adsLocation.getLongitude()*1E6;
- gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);
- }
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return gp;
- }
- private GeoPoint getGeoByAddress(String strSearchAddress)
- {
- GeoPoint gp = null;
- try
- {
- if(strSearchAddress!="")
- {
- Geocoder mGeocoder01 = new Geocoder
- (EX09_07.this, Locale.getDefault());
-
- List<Address> lstAddress = mGeocoder01.getFromLocationName
- (strSearchAddress, 1);
- if (!lstAddress.isEmpty())
- {
- Address adsLocation = lstAddress.get(0);
- double geoLatitude = adsLocation.getLatitude()*1E6;
- double geoLongitude = adsLocation.getLongitude()*1E6;
- gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);
- }
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return gp;
- }
private GeoPoint getGeoByAddress(String strSearchAddress){GeoPoint gp = null;try{if(strSearchAddress!=""){Geocoder mGeocoder01 = new Geocoder(EX09_07.this, Locale.getDefault());List<Address> lstAddress = mGeocoder01.getFromLocationName(strSearchAddress, 1);if (!lstAddress.isEmpty()){Address adsLocation = lstAddress.get(0);double geoLatitude = adsLocation.getLatitude()*1E6;double geoLongitude = adsLocation.getLongitude()*1E6;gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);}}}catch (Exception e){e.printStackTrace();}return gp;}
地圖放大縮小按鈕 -
- mButton02 = (Button)findViewById(R.id.myButton2);
- mButton02.setOnClickListener(new Button.OnClickListener()
- {
-
- public void onClick(View v)
- {
- intZoomLevel++;
- if(intZoomLevel>mMapView01.getMaxZoomLevel())
- {
- intZoomLevel = mMapView01.getMaxZoomLevel();
- }
- mMapController01.setZoom(intZoomLevel);
- }
- });
-
-
- mButton03 = (Button)findViewById(R.id.myButton3);
- mButton03.setOnClickListener(new Button.OnClickListener()
- {
-
- public void onClick(View v)
- {
- intZoomLevel--;
- if(intZoomLevel<1)
- {
- intZoomLevel = 1;
- }
- mMapController01.setZoom(intZoomLevel);
- }
- });
-
- mButton02 = (Button)findViewById(R.id.myButton2);
- mButton02.setOnClickListener(new Button.OnClickListener()
- {
-
- public void onClick(View v)
- {
- intZoomLevel++;
- if(intZoomLevel>mMapView01.getMaxZoomLevel())
- {
- intZoomLevel = mMapView01.getMaxZoomLevel();
- }
- mMapController01.setZoom(intZoomLevel);
- }
- });
-
-
- mButton03 = (Button)findViewById(R.id.myButton3);
- mButton03.setOnClickListener(new Button.OnClickListener()
- {
-
- public void onClick(View v)
- {
- intZoomLevel--;
- if(intZoomLevel<1)
- {
- intZoomLevel = 1;
- }
- mMapController01.setZoom(intZoomLevel);
- }
- });
/* 放大Map的Button */mButton02 = (Button)findViewById(R.id.myButton2);mButton02.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){intZoomLevel++;if(intZoomLevel>mMapView01.getMaxZoomLevel()){intZoomLevel = mMapView01.getMaxZoomLevel();}mMapController01.setZoom(intZoomLevel);}});/* 縮小Map的Button */mButton03 = (Button)findViewById(R.id.myButton3);mButton03.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){intZoomLevel--;if(intZoomLevel<1){intZoomLevel = 1;}mMapController01.setZoom(intZoomLevel);}});
以下文章轉(zhuǎn)載:
http://marshal.easymorse.com/archives/2528 android location provider有兩個(gè):
* LocationManager.GPS_PROVIDER:GPS,精度比較高,但是慢而且消耗電力,而且可能因?yàn)樘鞖庠蚧蛘哒系K物而無(wú)法獲取衛(wèi)星信息,另外設(shè)備可能沒(méi)有GPS模塊;
* LocationManager.NETWORK_PROVIDER:通過(guò)網(wǎng)絡(luò)獲取定位信息,精度低,耗電少,獲取信息速度較快,不依賴(lài)GPS模塊。
為了程序的通用性,希望動(dòng)態(tài)選擇location provider。對(duì)android通過(guò)Location API顯示地址信息做了個(gè)別改動(dòng),可以看到使用了gps定位,精度較高:
這里使用到了Criteria,可根據(jù)當(dāng)前設(shè)備情況自動(dòng)選擇哪種location provider。見(jiàn)
- LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
-
- Criteria criteria = new Criteria();
- criteria.setAccuracy(Criteria.ACCURACY_FINE);
- criteria.setAltitudeRequired(false);
- criteria.setBearingRequired(false);
- criteria.setCostAllowed(true);
- criteria.setPowerRequirement(Criteria.POWER_LOW);
-
- location = locationManager
- .getLastKnownLocation(locationManager.getBestProvider(criteria, true));
- LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
-
- Criteria criteria = new Criteria();
- criteria.setAccuracy(Criteria.ACCURACY_FINE);
- criteria.setAltitudeRequired(false);
- criteria.setBearingRequired(false);
- criteria.setCostAllowed(true);
- criteria.setPowerRequirement(Criteria.POWER_LOW);
-
- location = locationManager
- .getLastKnownLocation(locationManager.getBestProvider(criteria, true));
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);Criteria criteria = new Criteria();criteria.setAccuracy(Criteria.ACCURACY_FINE);// 設(shè)置為最大精度criteria.setAltitudeRequired(false);//不要求海拔信息criteria.setBearingRequired(false);// 不要求方位信息criteria.setCostAllowed(true);//是否允許付費(fèi)criteria.setPowerRequirement(Criteria.POWER_LOW);// 對(duì)電量的要求location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, true));
原來(lái)的寫(xiě)法很簡(jiǎn)單:
- LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
-
- location=locationManager.getLastKnownLocation(LocationManager.NETWORK
- LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
-
- location=locationManager.getLastKnownLocation(LocationManager.NETWORK)
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);location=locationManager.getLastKnownLocation(LocationManager.NETWORK)