从一个活动发送您的当前位置到另一个位置 [英] Send your current location from one activity to another
问题描述
我想获得我的lčocation在一个活动,然后将其发送到另一个(当我点击按钮),而且在地图上显示我的位置。 这是我的code(问题是,地图不动画我目前的位置,我相信第一个活动没有获得当前位置和发送空到次活动):
公共类ActivityMain扩展MapActivity {
私人LocationManager locationManager;
私人LocationListener的LocationListener的;
私人的GeoPoint currentGeoPoint = NULL;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activitymain);
尝试{
locationManager =(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
LocationListener的=新的getLocation();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,LocationListener的);
}
赶上(NullPointerException异常E){
的System.out.println(空);
e.printStackTrace();
}
赶上(例外五){
e.printStackTrace();
}
}
公共无效onButton1Click(查看视图){
意向意图=新的意图(ActivityMain.this,ActivityMap.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
如果(currentGeoPoint!= NULL){
intent.putExtra(纬度,currentGeoPoint.getLatitudeE6());
intent.putExtra(长,currentGeoPoint.getLongitudeE6());
}
其他{
尝试{
locationManager =(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
LocationListener的=新的getLocation();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,LocationListener的);
}
赶上(NullPointerException异常E){
的System.out.println(空);
e.printStackTrace();
}
赶上(例外五){
e.printStackTrace();
}
}
上下文C = getApplicationContext();
c.startActivity(意向);
//意向意图=新的意图(ActivityMain.this,ActivityMap.class);
//this.startActivity(intent);
}
公共无效onButton2Clik(查看视图){
意向意图=新的意图(ActivityMain.this,ActivityListCategories.class);
this.startActivity(意向);
}
一流的getLocation实现LocationListener的{
公共无效onLocationChanged(位置定位){
如果(位置!= NULL){
的GeoPoint currentPoint = getCurrentPoint(位置);
currentGeoPoint = currentPoint;
}
}
公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
// TODO自动生成方法存根
}
公共无效onProviderEnabled(字符串提供商){
Toast.makeText(getApplicationContext(),getProvider()+使能,Toast.LENGTH_SHORT).show();
}
公共无效onProviderDisabled(字符串提供商){
Toast.makeText(getApplicationContext(),getProvider()+已禁用,Toast.LENGTH_SHORT).show();
}
}
公众的GeoPoint getLastKnownPoint(){
的GeoPoint lastKnownPoint = NULL;
位置lastKnownLocation = locationManager.getLastKnownLocation(getProvider());
如果(lastKnownLocation!= NULL){
lastKnownPoint = getCurrentPoint(lastKnownLocation);
}
返回lastKnownPoint;
}
公众的GeoPoint getCurrentPoint(位置定位){
的GeoPoint currentPoint =新的GeoPoint((INT)(location.getLatitude()* 1E6),(INT)(location.getLongitude()* 1E6));
返回currentPoint;
}
公共字符串getProvider(){
locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
标准标准=新标准();
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
criteria.setAccuracy(Criteria.NO_REQUIREMENT);
字符串商= locationManager.getBestProvider(标准,真正的);
返回供应商;
}
@覆盖
保护的布尔isRouteDisplayed(){
// TODO自动生成方法存根
返回false;
}
保护无效onResume(){
super.onResume();
LocationManager newLocationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager = newLocationManager;
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,LocationListener的);
}
保护无效的onPause(){
super.onPause();
locationManager.removeUpdates(LocationListener的);
}
保护无效的onDestroy(){
super.onDestroy();
locationManager.removeUpdates(LocationListener的);
}
}
这里是code的第二个活动
公共类ActivityMap扩展MapActivity {
私人MapController mapController;
私人图形页面图形页面;
私人LocationManager locationManager;
私人LocationListener的LocationListener的;
私人的GeoPoint currentGeoPoint;
私人的位置currentLocation = NULL;
私人ClassMapOverlay currPos;
私人ClassCustomItemizedOverlay< ClassCustomOverlayItem> mallsOverlay;
私人列表< ClassMall>商场;
// TODO:AsyncTask的
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activitymap);
束束= getIntent()getExtras()。
如果(捆绑!= NULL){
currentGeoPoint =新的GeoPoint((INT)(bundle.getDouble(土地增值税)/ 1E6),(INT)(bundle.getDouble(长)/ 1E6));
animateToCurrentPoint(currentGeoPoint);
}
图形页面=(图形页面)findViewById(R.id.mapView);
mapController = mapView.getController();
公众的GeoPoint getLastKnownPoint(){
的GeoPoint lastKnownPoint = NULL;
位置lastKnownLocation = locationManager.getLastKnownLocation(getProvider());
如果(lastKnownLocation!= NULL){
lastKnownPoint = getCurrentPoint(lastKnownLocation);
}
返回lastKnownPoint;
}
公众的GeoPoint getCurrentPoint(位置定位){
的GeoPoint currentPoint =新的GeoPoint((INT)(location.getLatitude()* 1E6),(INT)(location.getLongitude()* 1E6));
返回currentPoint;
}
公共无效animateToCurrentPoint(GeoPoint对象currentPoint){
mapController.animateTo(currentPoint);
mapController.setCenter(currentPoint);
mapController.setZoom(15);
}
公共无效drawCurrPositionOverlay(){
名单<覆盖>叠加=调用MapView.getOverlays();
overlays.remove(currPos);
绘制对象标记= getResources()getDrawable(R.drawable.me)。
currPos =新ClassMapOverlay(标志,图形页面);
的GeoPoint drawMyPoint = NULL;
如果(currentGeoPoint == NULL){
drawMyPoint = getLastKnownPoint();
}
其他 {
drawMyPoint = currentGeoPoint;
}
OverlayItem overlayitem =新OverlayItem(drawMyPoint,莫哈adresa:,的getAddress(currentLocation));
currPos.addOverlay(overlayitem);
overlays.add(currPos);
currPos.setCurrentLocation(currentLocation);
}
公共无效drawMallsOverlay(名单< ClassMall>商场){
名单<覆盖>叠加=调用MapView.getOverlays();
overlays.remove(mallsOverlay);
绘制对象标记= getResources()getDrawable(R.drawable.malls)。
mallsOverlay =新ClassCustomItemizedOverlay< ClassCustomOverlayItem>(标志,图形页面);
如果(malls.size()大于0){
对于(ClassMall温度:商场){
的GeoPoint mallPoint = getLatLon(temp.getAddress());
ClassCustomOverlayItem overlayItem =新ClassCustomOverlayItem(mallPoint,temp.getName(),temp.getAddress(),temp.getUrl());
mallsOverlay.addOverlay(overlayItem);
//Toast.makeText(getApplicationContext(),温度,Toast.LENGTH_SHORT).show();
}
}
overlays.add(mallsOverlay);
}
公共字符串getProvider(){
locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
标准标准=新标准();
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
criteria.setAccuracy(Criteria.NO_REQUIREMENT);
字符串商= locationManager.getBestProvider(标准,真正的);
返回供应商;
}
公共字符串的getAddress(位置定位){
地理codeR地理codeR =新的地缘codeR(getApplicationContext(),Locale.getDefault());
字符串sAddress =;
尝试{
名单<地址>地址=地理coder.getFromLocation(location.getLatitude(),location.getLongitude(),1);
如果(address.size()大于0){
的for(int i = 0; I< address.get(0).getMaxAddressLineIndex();我++){
sAddress + = address.get(0).getAddressLine(ⅰ)+\ N的;
}
}
}
赶上(IOException异常E){
e.printStackTrace();
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_SHORT).show();
}
返回sAddress;
}
公众的GeoPoint getLatLon(字符串地址){
地理codeR地理codeR =新的地缘codeR(这一点,Locale.getDefault());
的GeoPoint点= NULL;
名单<地址>地址= NULL;
尝试 {
地址=地理coder.getFromLocationName(地址,1);
如果(addresses.size()大于0){
点=新的GeoPoint((INT)(addresses.get(0).getLatitude()* 1E6)
(中间体)(addresses.get(0).getLongitude()* 1E6));
}
}赶上(IOException异常E){
e.printStackTrace();
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_SHORT).show();
}
返回点;
}
@覆盖
保护的布尔isRouteDisplayed(){
// TODO自动生成方法存根
返回false;
}
}
和请你能告诉我,我怎么能异步获得我的当前位置?
LocationManager LM =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
位置= lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
意图=新的意图(LegalSeeFoods.this,MapDemoActivity.class);
intent.putExtra(土地增值税,位置.latitude);
intent.putExtra(长,位置.longitude);
startActivity(意向);
在 MapDemoActivity.class
mapView.getController()setCenter(点);
< --- 以动画为位置
i would like to obtain my lčocation in one activity, then send it to another (when i click button) and there show my location on maps. Here's my code (the problem is,map doesn't animate to my current location, I believe first activity doesn't obtain current location and sends null to second activity):
public class ActivityMain extends MapActivity {
private LocationManager locationManager;
private LocationListener locationListener;
private GeoPoint currentGeoPoint = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitymain);
try{
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new getLocation();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
catch (NullPointerException e){
System.out.println("Null");
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
public void onButton1Click(View view) {
Intent intent = new Intent(ActivityMain.this, ActivityMap.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if(currentGeoPoint != null){
intent.putExtra("lat", currentGeoPoint.getLatitudeE6());
intent.putExtra("long", currentGeoPoint.getLongitudeE6());
}
else{
try{
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new getLocation();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
catch (NullPointerException e){
System.out.println("Null");
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
Context c = getApplicationContext();
c.startActivity(intent);
//Intent intent = new Intent(ActivityMain.this, ActivityMap.class);
//this.startActivity(intent);
}
public void onButton2Clik (View view){
Intent intent = new Intent(ActivityMain.this, ActivityListCategories.class);
this.startActivity(intent);
}
class getLocation implements LocationListener {
public void onLocationChanged(Location location) {
if (location != null){
GeoPoint currentPoint = getCurrentPoint(location);
currentGeoPoint = currentPoint;
}
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), getProvider() + " enabled", Toast.LENGTH_SHORT).show();
}
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), getProvider() + " disabled", Toast.LENGTH_SHORT).show();
}
}
public GeoPoint getLastKnownPoint (){
GeoPoint lastKnownPoint = null;
Location lastKnownLocation = locationManager.getLastKnownLocation(getProvider());
if(lastKnownLocation != null){
lastKnownPoint = getCurrentPoint(lastKnownLocation);
}
return lastKnownPoint;
}
public GeoPoint getCurrentPoint (Location location){
GeoPoint currentPoint = new GeoPoint((int)(location.getLatitude()*1E6),(int)(location.getLongitude()*1E6));
return currentPoint;
}
public String getProvider() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
criteria.setAccuracy(Criteria.NO_REQUIREMENT);
String Provider = locationManager.getBestProvider(criteria, true);
return Provider;
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
protected void onResume(){
super.onResume();
LocationManager newLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager = newLocationManager;
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
protected void onPause(){
super.onPause();
locationManager.removeUpdates(locationListener);
}
protected void onDestroy(){
super.onDestroy();
locationManager.removeUpdates(locationListener);
}
}
and here is code for second activity
public class ActivityMap extends MapActivity {
private MapController mapController;
private MapView mapView;
private LocationManager locationManager;
private LocationListener locationListener;
private GeoPoint currentGeoPoint;
private Location currentLocation = null;
private ClassMapOverlay currPos;
private ClassCustomItemizedOverlay<ClassCustomOverlayItem> mallsOverlay;
private List<ClassMall> malls;
// TODO: AsyncTAsk
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitymap);
Bundle bundle = getIntent().getExtras();
if(bundle != null){
currentGeoPoint = new GeoPoint((int)(bundle.getDouble("lat") / 1E6),(int)(bundle.getDouble("long")/1E6));
animateToCurrentPoint(currentGeoPoint);
}
mapView = (MapView) findViewById(R.id.mapView);
mapController = mapView.getController();
public GeoPoint getLastKnownPoint (){
GeoPoint lastKnownPoint = null;
Location lastKnownLocation = locationManager.getLastKnownLocation(getProvider());
if(lastKnownLocation != null){
lastKnownPoint = getCurrentPoint(lastKnownLocation);
}
return lastKnownPoint;
}
public GeoPoint getCurrentPoint (Location location){
GeoPoint currentPoint = new GeoPoint((int)(location.getLatitude()*1E6),(int)(location.getLongitude()*1E6));
return currentPoint;
}
public void animateToCurrentPoint(GeoPoint currentPoint){
mapController.animateTo(currentPoint);
mapController.setCenter(currentPoint);
mapController.setZoom(15);
}
public void drawCurrPositionOverlay(){
List<Overlay> overlays = mapView.getOverlays();
overlays.remove(currPos);
Drawable marker = getResources().getDrawable(R.drawable.me);
currPos = new ClassMapOverlay(marker,mapView);
GeoPoint drawMyPoint = null;
if(currentGeoPoint==null){
drawMyPoint = getLastKnownPoint();
}
else {
drawMyPoint = currentGeoPoint;
}
OverlayItem overlayitem = new OverlayItem(drawMyPoint, "Moja adresa:", getAddress(currentLocation));
currPos.addOverlay(overlayitem);
overlays.add(currPos);
currPos.setCurrentLocation(currentLocation);
}
public void drawMallsOverlay(List <ClassMall> malls){
List <Overlay> overlays = mapView.getOverlays();
overlays.remove(mallsOverlay);
Drawable marker = getResources().getDrawable(R.drawable.malls);
mallsOverlay = new ClassCustomItemizedOverlay<ClassCustomOverlayItem>(marker, mapView);
if(malls.size() > 0){
for(ClassMall temp: malls ){
GeoPoint mallPoint = getLatLon(temp.getAddress());
ClassCustomOverlayItem overlayItem = new ClassCustomOverlayItem(mallPoint,temp.getName(),temp.getAddress(), temp.getUrl());
mallsOverlay.addOverlay(overlayItem);
//Toast.makeText(getApplicationContext(), temp, Toast.LENGTH_SHORT).show();
}
}
overlays.add(mallsOverlay);
}
public String getProvider() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
criteria.setAccuracy(Criteria.NO_REQUIREMENT);
String Provider = locationManager.getBestProvider(criteria, true);
return Provider;
}
public String getAddress (Location location){
Geocoder geoCoder = new Geocoder(getApplicationContext(), Locale.getDefault());
String sAddress = "";
try{
List <Address> address = geoCoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if(address.size() > 0){
for(int i = 0; i < address.get(0).getMaxAddressLineIndex(); i++){
sAddress += address.get(0).getAddressLine(i) + "\n";
}
}
}
catch (IOException e){
e.printStackTrace();
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
return sAddress;
}
public GeoPoint getLatLon (String address){
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
GeoPoint point = null;
List <Address> addresses = null;
try {
addresses = geoCoder.getFromLocationName(address, 1);
if(addresses.size() > 0){
point = new GeoPoint((int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude()*1E6));
}
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
return point;
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
And please can you tell me how can i asynchronously obtain my current location ?
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
intent = new Intent(LegalSeeFoods.this, MapDemoActivity.class);
intent.putExtra("lat", location .latitude);
intent.putExtra("long", location .longitude);
startActivity(intent);
IN MapDemoActivity.class
mapView.getController().setCenter(point);
<--- TO animate to location
这篇关于从一个活动发送您的当前位置到另一个位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!