2011-04-19 21 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プロジェクト 私は私のアプリを実行すると、それが正常に動作しますが、 に地図上のマーカーを置くためにどのように私は場所がTelnet throgh座標送信する際には、ブルースクリーンが表示され、ここにマーカーがありません pls help me場所はアンドロイド

答えて

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(); 

これは、ユーザーが現在位置と更新が表示されます。ユーザーがどこにいたのか分かりません。 (これはあなたが達成しようとか、単に現在位置を表示しているものであれば、私はよく分からない。)onLocationChangedメソッド内でごMyLocationクラスで

2

は、あなたの代わりにGeoPoint p = new GeoPoint(lat, lon);p = new GeoPoint(lat, lon);を試し書きする必要があります。あなたのコードでは、p(グローバル変数と同じ)という新しい変数を作成し、ローカル変数ポイントをオブジェクトに作成しました。あなたのローカル変数pが何かを指していないところ(= null)。グローバル変数pを描画に使用し、nullに設定されているためです。

関連する問題