是这样的 开始的时候我已经设定了我现在的位置 通过一系列操作 我找到了我想要去的地方 并且得到了它的经纬度 回到了主页面(主页面是一张地图 中心是我的位置) 现在我要做的是 首先能显示它的位置 齐次是在我的位置和它的位置之间 有一条最短路径 代码如下:
public class hereami extends MapActivity implements LocationListener {// 这里已经实现了LocationListener接口
// 所以不用再定义了
/** Called when the activity is first created. */ //private MapView mapView;
private LocationManager lm;
String bestProvider;
double getlatitude,getlongitude;
private MapController mc;
private GeoPoint gp,newgp;
MyPositionOverlay positionOverlay;
int getI;
// private Paint paint;
// private Point myScreenCoords;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapView);
mc = mapView.getController();//记住要早点得到MapControl
//一下是从别的Activity中传过来的经纬度
Intent descriptionTohereami = getIntent();
getI = descriptionTohereami.getIntExtra("I", getI);
System.out.println(""+getI);
if(getI==1){
getlatitude = descriptionTohereami.getDoubleExtra("latitude", getlatitude);
getlongitude = descriptionTohereami.getDoubleExtra("longitude", getlongitude);
System.out.println(""+getI+" "+getlatitude+" "+getlongitude);
//Location a = new Location(a);
newgp = new GeoPoint((int)(getlatitude*1e6), (int)(getlongitude*1e6));
System.out.println(newgp.toString());
mc.animateTo(newgp);//这句失败了
mc.setZoom(17);
}
我试图将屏幕聚焦到传过来的经纬度的位置 失败了 说我mc.animateTo(newgp):空指针异常 以下是我 初始条件下获得位置的代码:
//选择服务器 和条件
m = (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 = lm.getBestProvider(criteria, true);// 将符合条件的Provider放在字符串bestProvider中
Location myLocation = lm.getLastKnownLocation(bestProvider);
updateWithNewLocation(myLocation);// 这个函数与更新无关,是用于首次定位地图使用的函数,是功能的封装
lm.requestLocationUpdates(bestProvider, 60000, 10, this);// 设置地图更新,若条件成立,调用LocationListener监听器中相应的方法
}//显示位置
private void updateWithNewLocation(Location myLocation) {
// TODO Auto-generated method stub
if (myLocation != null) {
// Update my location er
positionOverlay.setLocation(myLocation);
// Update the map location.
gp = new GeoPoint((int) (myLocation.getLatitude() * 1e6),
(int) (myLocation.getLongitude() * 1e6));
mc.animateTo(gp);
mc.setZoom(17);
} else {
Toast.makeText(this, "No location found", Toast.LENGTH_LONG).show();
}
}我就是按照这个写的 但是不对 往高人指点!!我不是要得到新地址的聚焦 先帮我显示出来 然后再画最短路径最好了! 下边是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 MyPositionOverlay 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;
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 er
canvas.drawOval(oval, paint);
canvas.drawRoundRect(backRect, 5, 5, backPaint);
canvas.drawText("I'm Here",
point.x + 2*mRadius, point.y,
paint);
}
super.draw(canvas, mapView, shadow);
}
}
public class hereami extends MapActivity implements LocationListener {// 这里已经实现了LocationListener接口
// 所以不用再定义了
/** Called when the activity is first created. */ //private MapView mapView;
private LocationManager lm;
String bestProvider;
double getlatitude,getlongitude;
private MapController mc;
private GeoPoint gp,newgp;
MyPositionOverlay positionOverlay;
int getI;
// private Paint paint;
// private Point myScreenCoords;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapView);
mc = mapView.getController();//记住要早点得到MapControl
//一下是从别的Activity中传过来的经纬度
Intent descriptionTohereami = getIntent();
getI = descriptionTohereami.getIntExtra("I", getI);
System.out.println(""+getI);
if(getI==1){
getlatitude = descriptionTohereami.getDoubleExtra("latitude", getlatitude);
getlongitude = descriptionTohereami.getDoubleExtra("longitude", getlongitude);
System.out.println(""+getI+" "+getlatitude+" "+getlongitude);
//Location a = new Location(a);
newgp = new GeoPoint((int)(getlatitude*1e6), (int)(getlongitude*1e6));
System.out.println(newgp.toString());
mc.animateTo(newgp);//这句失败了
mc.setZoom(17);
}
我试图将屏幕聚焦到传过来的经纬度的位置 失败了 说我mc.animateTo(newgp):空指针异常 以下是我 初始条件下获得位置的代码:
//选择服务器 和条件
m = (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 = lm.getBestProvider(criteria, true);// 将符合条件的Provider放在字符串bestProvider中
Location myLocation = lm.getLastKnownLocation(bestProvider);
updateWithNewLocation(myLocation);// 这个函数与更新无关,是用于首次定位地图使用的函数,是功能的封装
lm.requestLocationUpdates(bestProvider, 60000, 10, this);// 设置地图更新,若条件成立,调用LocationListener监听器中相应的方法
}//显示位置
private void updateWithNewLocation(Location myLocation) {
// TODO Auto-generated method stub
if (myLocation != null) {
// Update my location er
positionOverlay.setLocation(myLocation);
// Update the map location.
gp = new GeoPoint((int) (myLocation.getLatitude() * 1e6),
(int) (myLocation.getLongitude() * 1e6));
mc.animateTo(gp);
mc.setZoom(17);
} else {
Toast.makeText(this, "No location found", Toast.LENGTH_LONG).show();
}
}我就是按照这个写的 但是不对 往高人指点!!我不是要得到新地址的聚焦 先帮我显示出来 然后再画最短路径最好了! 下边是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 MyPositionOverlay 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;
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 er
canvas.drawOval(oval, paint);
canvas.drawRoundRect(backRect, 5, 5, backPaint);
canvas.drawText("I'm Here",
point.x + 2*mRadius, point.y,
paint);
}
super.draw(canvas, mapView, shadow);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货