当前位置:编程学习 > wap >>

关于获取当前位置的问题

  我编写了一个程序 主要实现2个功能 一是显示用户当前位置 二是查询周围商铺 现在第一个完成了 第二个还差点 主要差在怎么把查询出来的 在数据库中的经纬度 显示在地图上 过程是这样的:主界面(显示我当前的位置)——》查询界面(各种条件)——》返回所要查询位置的经纬度——》回到主界面显示商铺位置 并且连接路径
  但是在返回主界面的时候 虽然我传回了经纬度 但是手机适中聚焦在开始设定的位置不变,后来我又创建了一个文件 希望在这个文件里显示我传过来的经纬度 但是同样遇到了问题,代码如下:
public class destination extends MapActivity implements LocationListener{
double getlatitude,getlongitude;
private LocationManager locationmanager;
String bestProvider;
private MapController mapcontroller;
private GeoPoint geopoint;
MyPositionOverlay positionOverlay;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.destination);

MapView destination_mapView = (MapView) findViewById(R.id.destination_mapView);
mapcontroller = destination_mapView.getController();
destination_mapView.setBuiltInZoomControls(true);
destination_mapView.setTraffic(true);
// Add the MyPositionOverlay
    positionOverlay = new MyPositionOverlay();
    List<Overlay> overlays = destination_mapView.getOverlays();
    overlays.add(positionOverlay);
    
locationmanager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);// 低精度
criteria.setPowerRequirement(Criteria.POWER_LOW);// 低能耗
criteria.setAltitudeRequired(false);// 不要求海拔
criteria.setBearingRequired(false);// 不要求方向
criteria.setSpeedRequired(false);// 不要求速度
bestProvider = locationmanager.getBestProvider(criteria, true);// 将符合条件的Provider放在字符串bestProvider中
//Location destination = locationmanager.主要就是这句!!!!这句!!!看到没就这句!!!

Intent descriptionTodestination = getIntent();
getlatitude = descriptionTodestination.getDoubleExtra("latitude", getlatitude);
getlongitude = descriptionTodestination.getDoubleExtra("longitude", getlongitude);
//System.out.println(getlatitude+" "+getlongitude);

}
在得到Provider的时候 调用locationmanager的方法,但是我不知道调用什么方法啊?下边是传过来的经纬度,究竟怎么能让屏幕聚焦到传递过来的经纬度的位置 而不是在DDMS中一次次的设定啊!!!! --------------------编程问答-------------------- 帮你顶,我也学习下 --------------------编程问答-------------------- 谢谢 不过还是没人回答 2天了 --------------------编程问答-------------------- 不懂,围观学习下 --------------------编程问答-------------------- 你可以看看红色字体部分代码,他就是根据经纬度聚焦到屏幕中心的
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

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;


public class MyLocation extends MapActivity{

private MapView mMapView;
private MapController mMapController;
private MyLocationOverlay myPosition;
private String GpsData;

@Override
protected void onCreate(Bundle icicle) {
// TODO Auto-generated method stub
super.onCreate(icicle);
setContentView(R.layout.main);
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager) getSystemService(context);
mMapView = (MapView) findViewById(R.id.map);
mMapView.setTraffic(false);
mMapView.setSatellite(true);
mMapView.setStreetView(false);
mMapController = mMapView.getController();
mMapView.setEnabled(true);
mMapView.setClickable(true);
mMapView.setBuiltInZoomControls(true);
mMapController.setZoom(17);
myPosition = new MyLocationOverlay();
List<Overlay> overlays = mMapView.getOverlays();
overlays.add(myPosition);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 3000, 0, locationListener);
}

private void updateWithNewLocation(Location location){
String latLongString;
TextView myLocationText = (TextView) findViewById(R.id.TextView01); 
String addressString = "没有找到地址\n";
if (location != null) {
myPosition.setMLocation(location);
Double geoLat = location.getLatitude()*1E6;
Double geoLng = location.getLongitude()*1E6;
GeoPoint point = new GeoPoint(geoLat.intValue(), geoLng.intValue());
mMapController.animateTo(point);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
latLongString = "经度:" + latitude + "\n纬度:" + longitude;
GpsData = latLongString;
Geocoder gc = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = gc.getFromLocation(latitude, longitude, 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0) {
Address address = addresses.get(0);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName()).append("\n");
addressString = sb.toString();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {
latLongString = "没有找到坐标:\n";
}
myLocationText.setText("你当前的坐标如下:\n" + latLongString 
+ "\n" + addressString);
}

private final LocationListener locationListener = new LocationListener(){
public void onLocationChanged(Location location){
updateWithNewLocation(location);
}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
updateWithNewLocation(null);
}

@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

}
};

@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
   
class MyLocationOverlay extends Overlay{

Location mLocation;

public void setMLocation(Location location) {
mLocation = location;
}

@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
// TODO Auto-generated method stub
super.draw(canvas, mapView, shadow);
Paint paint = new Paint();
Point myScreenCoords = new Point();
GeoPoint tmpGeoPoint = new GeoPoint((int)(mLocation.getLatitude()*1E6),
(int)(mLocation.getLongitude()*1E6));
mapView.getProjection().toPixels(tmpGeoPoint, myScreenCoords);
paint.setStrokeWidth(1);
paint.setARGB(255, 255, 0, 0);
paint.setStyle(Paint.Style.STROKE);
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
android.R.drawable.btn_star_big_on);
canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
canvas.drawText("here am I", myScreenCoords.x, myScreenCoords.y, paint);
return true;

}
}} --------------------编程问答--------------------     你这个location 是通过Location location = locationManager.getLastKnownLocation(provider); 这句话 注意这个getLastKnownLocation这个函数 是返回当前位置的 不是返回我制定的位置 我不是要返回DDMS设定的位置 而是通过intent传过来的两个double型的变量 
    你写的程序将已经设定了经纬度的location通过updateWithNewLocation(location);传到函数中 在这个函数中的myPosition是OverLay类型的 通过myPosition.setMyLocation(location)来设定图层所图的位置 与上边传入的location位置保持一致 也就是说 地图本身是给定的位置 图层图的也是该位置
    主要就是这个getLastKnownLocation函数 返回的是在DDMS中设定的位置啊!我不要这样的 不过也谢谢你 这个代码我见过 书上的 我写过 --------------------编程问答-------------------- 你看看这个啊
GeoPoint tmpGeoPoint = new GeoPoint((int)(mLocation.getLatitude()*1E6),
(int)(mLocation.getLongitude()*1E6));
你把经纬度传到这里面不就行了,都叫你看红色字体部分了
--------------------编程问答-------------------- 这个方法我写过 不好使 我是这样写的 流程这样
GeoPoint tmpGeoPoint = new GeoPoint((int)(getLatitude()*1E6),
(int)(getLongitude()*1E6)); 里边的getLatitude和getLongitude是通过intent传过来的值 然后在 有一个什么来的 我给你粘贴 mapcontroller.animateTo(gp); mapcontroller.setZoom(17);来聚焦到这个点上 但是有编译错误 没通过 --------------------编程问答-------------------- 现在的主要问题 是我无法得到location 你给我写的代码是通过getLastKnownLoaction来获得当前位置的 我要的是通过我传递过来的经纬度来定义一个新的location 你看上边那个gp里边的值 不都是通过location 或者 mylocation来取得经纬度的吗? --------------------编程问答-------------------- 这样写就可以把你传进去的经纬度设置在屏幕中心位置了,
MapController controller;
GeoPoint station_beijing = new GeoPoint(
                        (int)(39.928416*1000000),
(int)(116.139729*1000000)
controller.animateTo(station_beijing);
--------------------编程问答-------------------- GeoPoint station_beijing = new GeoPoint(
  (int)(39.928416*1000000),
(int)(116.139729*1000000);
这句少了个分号
--------------------编程问答-------------------- 貌似少发了点东西
map = (MapView) findViewById(R.id.map);
controller = map.getController(); --------------------编程问答-------------------- 直接写数值啊……那我和用double变量代替数值有什么区别啊…… 我输出过传过来的getLatitude和getLongitude,输出的数值就是我要的 我的代码只不过是写的变量 现在全乱套了 又说我数据库没关 在onDestory中写了关闭有 又说我图层里边代码有问题 确实是有问题 我的图层传进去的是Location类型的变量,通过Location.latitude和location.longitude得到经纬度的 啊啊啊啊……后天交 我疯了 --------------------编程问答-------------------- 我解决了一半
在新的地方的图层 我还是用旧的涂层文件 看来不行 因为久的是有Location的 但是我新的地方不知道怎么创建Location中的经纬度 所以无法用location.latitude 和 location.longitude来得到经纬度 看来我要重新写涂层文件了 不过不合理啊……谁知道怎么把经纬度设定在一个location里边啊!!这是问题的关键! --------------------编程问答-------------------- 试试location.setLatitude(double latitude);
location.setLongitude(double longitude);
--------------------编程问答-------------------- 貌似不好使 抛出空指针异常信息 怪了 我输出了latitude和longitude里边都有值啊 --------------------编程问答-------------------- 抛异常还能输出值啊?把异常贴来看看。 --------------------编程问答-------------------- destination.setLatitude(getlatitude);
destination.setLongitude(getlongitude);
mapView.setBuiltInZoomControls(true);
mapView.setTraffic(true);

destination_overlay = new destination_overlay();
//overlays.add(destination, object)
System.out.println(destination.toString());
positionOverlay.setLocation(destination);
System.out.println("123");

第一个输出可以输出位置 第二个123输出不了 下面是overlay代码
package fty.hereami;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.location.Location;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class destination_overlay extends Overlay {

  private final int mRadius = 5;

  Location location;
 
  public Location getLocation() {
    return location;
  }
  public void setLocation(Location location) {
    this.location = location;
  }

  @Override
  public boolean onTap(GeoPoint point, MapView mapView) {
    return false;
  }
  
  @Override
  public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    Projection projection = mapView.getProjection();

    if (shadow == false) {
      // Get the current location    
      Double latitude = location.getLatitude()*1E6;
      Double longitude = location.getLongitude()*1E6;
      //System.out.println(""+latitude+" "+longitude+123);
      GeoPoint geoPoint; 
      geoPoint = new 
        GeoPoint(latitude.intValue(),longitude.intValue());

      // Convert the location to screen pixels     
      Point point = new Point();
      projection.toPixels(geoPoint, point);

      RectF oval = new RectF(point.x - mRadius, point.y - mRadius, 
                             point.x + mRadius, point.y + mRadius);

      // Setup the paint
      Paint paint = new Paint();
      paint.setARGB(250, 255, 255, 255);
      paint.setAntiAlias(true);//好像是设置边缘平滑
      paint.setFakeBoldText(true);//设置假的黑体点 不知道什么意思

      Paint backPaint = new Paint();
      backPaint.setARGB(175, 50, 50, 50);
      backPaint.setAntiAlias(true);

      RectF backRect = new RectF(point.x + 2 + mRadius, 
                                 point.y - 3*mRadius,
                                 point.x + 65, point.y + mRadius);

      // Draw the marker    
      canvas.drawOval(oval, paint);
      canvas.drawRoundRect(backRect, 5, 5, backPaint);
      canvas.drawText("It's Here!", 
                      point.x + 2*mRadius, point.y, 
                      paint);
    }
    super.draw(canvas, mapView, shadow);
  }
}
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,