2011-04-19 26 views
1
package com.android.gps; 

import java.util.List; 

import com.android.gps.hellogps.MyLocation.MapOverlay; 
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.MapView.LayoutParams; 
import com.google.android.maps.Overlay; 

import android.app.Activity; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Point; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.LinearLayout; 
import android.widget.Toast; 

public class hellogps extends MapActivity { 
    /** Called when the activity is first created. */ 
    MapView mapView; 
    MapController mc; 
    GeoPoint p; 
    LocationManager locationManager; 
    hellogps x; 
    MapOverlay mapOverlay; 

    @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.linear); 
     View zoomView = mapView.getZoomControls(); 

     zoomLayout.addView(zoomView, 
      new LinearLayout.LayoutParams(
       LayoutParams.WRAP_CONTENT, 
       LayoutParams.WRAP_CONTENT)); 
     mapView.displayZoomControls(true); 

     mc = mapView.getController(); 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     LocationListener mlocListener = new MyLocation(); 


     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f, mlocListener); 





    } 



    @Override 
    protected boolean isRouteDisplayed() { 
     // TODO Auto-generated method stub 
     return false; 
    } 
public class MyLocation implements LocationListener 
{ 

    @Override 
    public void onLocationChanged(Location location) { 
     // TODO Auto-generated method stub 

     int lat = (int) (location.getLatitude() * 1E6); 
     int lng = (int) (location.getLongitude() * 1E6); 



     GeoPoint p = new GeoPoint(lat, lng); 

     mc.setZoom(17); 
      mc.animateTo(p); 
      List<Overlay> listOfOverlays = mapView.getOverlays(); 
      listOfOverlays.clear(); 
      listOfOverlays.add(mapOverlay);   

      mapView.invalidate(); 


     mapView.invalidate(); 

    } 
     class MapOverlay extends com.google.android.maps.Overlay 
     { 
      MapOverlay mapOverlay=new MapOverlay(); 

      @Override 
      public boolean draw(Canvas canvas, MapView mapView, 
      boolean shadow, long when) 
      { 
       super.draw(canvas, mapView, shadow);     

       //---translate the GeoPoint to screen pixels--- 
       Point screenPts = new Point(); 
       mapView.getProjection().toPixels(p, screenPts); 

       //---add the marker--- 
       Bitmap bmp = BitmapFactory.decodeResource(
        getResources(), R.drawable.push_pin);    
       canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);   
       return true; 
      } 
     } 

    @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 

    }}} 

即時通訊新得到改變把標記在地圖上做我的colege項目 當我運行我的應用程序,它工作正常,但 當我發送的位置座標throgh的telnet出現藍屏和沒有markar 請幫我如何當位置在安卓機器人

回答

1

如果你只是想在地圖上顯示用戶的當前位置,那麼使用內置的MyLocationOverlay類可能會更容易。

使用在你的onCreate()以下:

 MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView); 
     mapView.getOverlays().add(myLocationOverlay); 
     myLocationOverlay.enableCompass(); // if you want to display a compass also 
     myLocationOverlay.enableMyLocation(); 

這將顯示用戶的當前位置和更新,因爲他們走動。儘管用戶在哪裏,它並沒有保持一個軌道。 (我不知道這是否是你想要什麼來完成或只顯示當前位置。)

2

onLocationChanged方法中的MyLocation類,你應該寫的,而不是GeoPoint p = new GeoPoint(lat, lon);嘗試p = new GeoPoint(lat, lon);。在你的代碼中,你創建了一個叫做p的新變量(和全局變量相同),你的局部變量指向了對象。凡你的本地變量p不指向任何東西(= null)。因爲你在繪製中使用了你的全局變量p並且設置爲null。