我尝试每个代码和每个问题在这里找到错误每次我修复的东西 - 其他东西分解和方格框的网格覆盖所有地图如果我点击它所有我需要的是点击地图绘制标记和获取使用GPS在textview中输出地址,此输出看起来像 this ,我测试了我的互联网连接到模拟器,它的工作原理
这里是logcat错误:
01-23 03:37:52.868: E/MapActivity(309): Couldn't get connection factory client
01-23 03:37:53.979: E/QemuSensors(309): data__poll: len=-1, errno=9: Bad file number
01-23 03:37:53.979: E/QemuSensors(309): data__poll: len=-1, errno=9: Bad file number
01-23 03:37:53.989: E/QemuSensors(309): data__poll: len=-1, errno=9: Bad file number
01-23 03:37:53.989: E/QemuSensors(309): data__poll: len=-1, errno=9: Bad file number
这是代码:
public class Map extends MapActivity implements LocationListener {
MapView map = null;
long start;
long end;
MyLocationOverlay compass = null;
MapController c;
int x, y;
GeoPoint g;
Drawable d;
LocationManager n;
int lati = 0;
int longi = 0;
Customize cus;
Drawable drawable;
OverlayItem f;
Location location;
String add = "";
Criteria m;
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.map);
map = (MapView) findViewById(R.id.mapid);
map.getController();
map.setBuiltInZoomControls(true);
map.setStreetView(true);
map.getController().setZoom(19);
// map.setTraffic(true);
d = getResources().getDrawable(R.drawable.blue);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
drawable = this.getResources().getDrawable(R.drawable.red);
drawable.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
map.getOverlays().add(new Customize(d));
compass = new MyLocationOverlay(Map.this, map);
map.getOverlays().add(compass);
getLastLocation();
}
public void getLastLocation() {
String provider = getBestProvider();
location = n.getLastKnownLocation(provider);
if (location != null) {
setCurrentLocation(location);
} else {
Toast.makeText(this, "Location not yet acquired", Toast.LENGTH_LONG)
.show();
}
((TextView) findViewById(R.id.providerid)).setText("Provider :"
+ getBestProvider());
}
public String getBestProvider() {
n = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
m = new Criteria();
m.setPowerRequirement(Criteria.NO_REQUIREMENT);
m.setAccuracy(Criteria.NO_REQUIREMENT);
String bestProvider = n.getBestProvider(m, false);
return bestProvider;
}
@Override
protected void onPause() {
compass.disableCompass();
super.onPause();
n.removeUpdates(this);
}
@Override
protected void onResume() {
compass.enableCompass();
super.onResume();
n.requestLocationUpdates(getBestProvider(), 500, 1, this);
}
class maps extends Overlay {
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
// ---when user lifts his finger---
if (event.getAction() == 1) {
g = mapView.getProjection().fromPixels((int) event.getX(),
(int) event.getY());
Geocoder geoCoder = new Geocoder(getBaseContext(),
Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
g.getLatitudeE6() / 1E6, g.getLongitudeE6() / 1E6,
1);
if (addresses.size() > 0) {
for (int i = 0; i < addresses.get(0)
.getMaxAddressLineIndex(); i++)
add += addresses.get(0).getAddressLine(i) + "\n";
((TextView) findViewById(R.id.address)).setText(add);
}
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
return false;
}
/*
* public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
* long when) {
*
* try{ super.draw(canvas, mapView, shadow); //---translate the GeoPoint
* to screen pixels--- g= new GeoPoint( (int) (lati * 1E6), (int) (longi
* * 1E6)); Point screenPts = new Point();
* mapView.getProjection().toPixels(g, screenPts); //---add the
* marker--- Bitmap bmp = BitmapFactory.decodeResource( getResources(),
* R.drawable.blue); canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50,
* null); } catch(Exception h){
*
* h.printStackTrace(); } finally{
*
* map.setSatellite(!map.isSatellite());
*
* }
*
*
* return true; }
*/
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
public void setCurrentLocation(Location location) {
lati = (int) (location.getLatitude() * 1E6);
longi = (int) (location.getLongitude() * 1E6);
g = new GeoPoint(lati, longi);
map.getOverlays().add(new Customize(drawable));
}
@Override
public void onLocationChanged(Location arg0) {
setCurrentLocation(arg0);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(this, "Provider Disabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(this, "Provider Enabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
Toast.makeText(this, "Staus Changed", Toast.LENGTH_SHORT).show();
}
private GeoPoint getPoint(double lat, double lon) {
return (new GeoPoint((int) (lat * 1000000.0), (int) (lon * 1000000.0)));
}
public class Customize extends ItemizedOverlay<OverlayItem> {
ArrayList<OverlayItem> ar = new ArrayList<OverlayItem>();
private Drawable defaultMarker = null;
public Customize(Drawable defaultMarker) {
super(defaultMarker);
this.defaultMarker = defaultMarker;
boundCenterBottom(defaultMarker);
ar.add(new OverlayItem(getPoint(40.748963847316034,
-73.96807193756104), "UN", "United Nations"));
ar.add(new OverlayItem(getPoint(40.76866299974387,
-73.98268461227417), "Lincoln Center",
"Home of Jazz at Lincoln Center"));
ar.add(new OverlayItem(getPoint(40.765136435316755,
-73.97989511489868), "Carnegie Hall",
"Where you go with practice, practice, practice"));
ar.add(new OverlayItem(getPoint(40.70686417491799,
-74.01572942733765), "The Downtown Club",
"Original home of the Heisman Trophy"));
ar.add(new OverlayItem(getPoint(lati, longi), " ", " "));
populate();
}
@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return ar.get(i);
}
@Override
public int size() {
// TODO Auto-generated method stub
return ar.size();
}
protected boolean onTap(int i) {
Toast.makeText(Map.this, ar.get(i).getSnippet(), Toast.LENGTH_SHORT)
.show();
return (true);
}
}
}
清单:
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<uses-library android:name="com.google.android.maps"/>
<activity
android:label="@string/app_name"
android:name=".ShareActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="@string/app_name"
android:name=".Map" >
<intent-filter >
<action android:name="com.share.MAP" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
答案 0 :(得分:0)
您可以尝试降低缩放级别...可能缩放级别19在请求的位置不可用...您是否尝试在googlemaps中缩放到级别19以获取所请求的位置???
如果不是这样,请提供更多信息......您可以复制粘贴日志,以便提供更多详细信息...
答案 1 :(得分:0)
我发现我的地图密钥错误,这导致地图没有显示为白色网格
我需要将 c drive中的keytool路径添加到我的路径
Control Panel\All Control Panel
Items\System\Environment Variables
并在cmd中使用此命令获取密钥
keytool -list -alias androiddebugkey -keystore "Path to your debug.keystore" -storepass android -keypass android
最后我得到了正确的密钥:)