纬度和经度在android和地图上未显示(latitude and longitude change in android and map not shown)

编程入门 行业动态 更新时间:2024-10-19 02:14:48
纬度和经度在android和地图上未显示(latitude and longitude change in android and map not shown)

纬度和经度在android和地图上未显示

package net.learn2develop.GoogleMaps; import java.io.IOException; import java.util.*; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapActivity; import com.google.android.maps.MapController; import com.google.android.maps.MapView; import com.google.android.maps.Overlay; import com.google.android.maps.MapView.LayoutParams; import android.app.LocalActivityManager; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Point; import android.location.Address; import android.location.Geocoder; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.LinearLayout; import android.widget.Toast; public class MapsActivity extends MapActivity { MapView mapView; MapController mc; GeoPoint p; double latPoint, lngPoint; LocationManager myManager; class MapOverlay extends com.google.android.maps.Overlay { @Override public boolean onTouchEvent(MotionEvent event, MapView mapView) { //---when user lifts his finger--- if (event.getAction() == 1) { GeoPoint p = mapView.getProjection().fromPixels( (int) event.getX(), (int) event.getY()); Geocoder geoCoder = new Geocoder( getBaseContext(), Locale.getDefault()); try { List<Address> addresses = geoCoder.getFromLocation( p.getLatitudeE6() / 1E6, p.getLongitudeE6() / 1E6, 1); String add = ""; if (addresses.size() > 0) { for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++) add += addresses.get(0).getAddressLine(i) + "\n"; } Log.e("address", add); Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show(); } catch (IOException e) { e.printStackTrace(); } return true; } else return false; } } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mapView = (MapView) findViewById(R.id.mapView); LinearLayout zoomLayout = (LinearLayout) findViewById(R.id.zoom); View zoomView = mapView.getZoomControls(); zoomLayout.addView(zoomView, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); mapView.displayZoomControls(true); mc = mapView.getController(); LocationManager myManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); myManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,new myLocationListener()); mc.setZoom(10); //---Add a location marker--- MapOverlay mapOverlay = new MapOverlay(); List<Overlay> listOfOverlays = mapView.getOverlays(); listOfOverlays.clear(); listOfOverlays.add(mapOverlay); mapView.invalidate(); } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } class myLocationListener implements LocationListener { public void ListLocationUpdater() { } @Override public void onLocationChanged(Location loc) { if (myManager != null) { // List list = myManager.getAllProviders(); String param = (String) myManager.getProviders(true).get(0); loc = myManager.getLastKnownLocation(param); if (loc != null) { latPoint = loc.getLatitude(); lngPoint = loc.getLongitude(); p = new GeoPoint((int) (latPoint * 1E6), (int) (lngPoint * 1E6)); mc.animateTo(p); Log.e("RootDrawApplication",String.valueOf(latPoint)+" , "+String.valueOf(lngPoint)); } else Log.e("GoogleMaps ", "Error: Location is null"); } else Log.e("GoogleMaps ", "Error: Location Manager is null"); } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } }

}

latitude and longitude change in android and map not shown

package net.learn2develop.GoogleMaps; import java.io.IOException; import java.util.*; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapActivity; import com.google.android.maps.MapController; import com.google.android.maps.MapView; import com.google.android.maps.Overlay; import com.google.android.maps.MapView.LayoutParams; import android.app.LocalActivityManager; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Point; import android.location.Address; import android.location.Geocoder; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.LinearLayout; import android.widget.Toast; public class MapsActivity extends MapActivity { MapView mapView; MapController mc; GeoPoint p; double latPoint, lngPoint; LocationManager myManager; class MapOverlay extends com.google.android.maps.Overlay { @Override public boolean onTouchEvent(MotionEvent event, MapView mapView) { //---when user lifts his finger--- if (event.getAction() == 1) { GeoPoint p = mapView.getProjection().fromPixels( (int) event.getX(), (int) event.getY()); Geocoder geoCoder = new Geocoder( getBaseContext(), Locale.getDefault()); try { List<Address> addresses = geoCoder.getFromLocation( p.getLatitudeE6() / 1E6, p.getLongitudeE6() / 1E6, 1); String add = ""; if (addresses.size() > 0) { for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++) add += addresses.get(0).getAddressLine(i) + "\n"; } Log.e("address", add); Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show(); } catch (IOException e) { e.printStackTrace(); } return true; } else return false; } } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mapView = (MapView) findViewById(R.id.mapView); LinearLayout zoomLayout = (LinearLayout) findViewById(R.id.zoom); View zoomView = mapView.getZoomControls(); zoomLayout.addView(zoomView, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); mapView.displayZoomControls(true); mc = mapView.getController(); LocationManager myManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); myManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,new myLocationListener()); mc.setZoom(10); //---Add a location marker--- MapOverlay mapOverlay = new MapOverlay(); List<Overlay> listOfOverlays = mapView.getOverlays(); listOfOverlays.clear(); listOfOverlays.add(mapOverlay); mapView.invalidate(); } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } class myLocationListener implements LocationListener { public void ListLocationUpdater() { } @Override public void onLocationChanged(Location loc) { if (myManager != null) { // List list = myManager.getAllProviders(); String param = (String) myManager.getProviders(true).get(0); loc = myManager.getLastKnownLocation(param); if (loc != null) { latPoint = loc.getLatitude(); lngPoint = loc.getLongitude(); p = new GeoPoint((int) (latPoint * 1E6), (int) (lngPoint * 1E6)); mc.animateTo(p); Log.e("RootDrawApplication",String.valueOf(latPoint)+" , "+String.valueOf(lngPoint)); } else Log.e("GoogleMaps ", "Error: Location is null"); } else Log.e("GoogleMaps ", "Error: Location Manager is null"); } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } }

}

更多推荐

android,mapView,import,public,new,电脑培训,计算机培训,IT培训"/> <meta name=&

本文发布于:2023-08-06 20:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455738.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:经度   纬度   图上   android   latitude

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!