2011-12-05 15 views
2

* プログラムの開始時にジオポイントを現在地に設定する方法を教えてください。私は、あなたがここにいるとマークされたプログラムの開始時に、エミュレータ内のユーザの現在の位置にオーバーレイバルーンを設定したい。私のコードはhere- *アンドロイドの地図上の現在地にジオポイントを設定するにはどうすればよいですか?

package com.jbb; 

import java.util.List; 
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; 
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.MotionEvent; 
import android.widget.Toast; 


public class jbb_latitudeActivity extends MapActivity 
{  
    LocationManager lm; 
    MyLocationListener locationListener; 
    MapView mapView; 
    MapController mc; 
    GeoPoint geoPoint; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     try{ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     geoPoint = new GeoPoint((int) (12.949997* 1E6), (int) (80.140213 * 1E6)); 
     mapView = (MapView) findViewById(R.id.name); 
     mc = mapView.getController(); 
     mc.animateTo(geoPoint); 
     mapView.setClickable(true); 
     mapView.setBuiltInZoomControls(true); 
     mapView.setSatellite(true); 
     mapView.setTraffic(true); 
     List<Overlay> overlays = mapView.getOverlays(); 
       overlays.clear(); 
     overlays.add(new MyLocationListener()); 
     //---use the LocationManager class to obtain GPS locations--- 
     lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);  
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,new MyLocationListener()); 
     } 
    catch (Exception e) { 
     // TODO: handle exception 
     Toast.makeText(getApplicationContext(),e.toString() , 1).show(); 
    } 

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

    private class MyLocationListener extends Overlay implements LocationListener 
    { 
      public void draw(Canvas canvas, MapView mapView, boolean shadow) {        // 1 
      super.draw(canvas, mapView, shadow); 
       if (!shadow) {                    // 2 
        Point point = new Point(); 
        mapView.getProjection().toPixels(geoPoint, point);          // 3 
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.marker_default); // 4 
        int x = point.x - bmp.getWidth()/2;             // 5     inty=point.ybmp.getHeight();              // 6   canvas.drawBitmap(bmp,x,y,null);              // 7 
       } 
      } 
     public boolean onTouchEvent(MotionEvent event, MapView mapView) 
     { 
      //---when user lifts his finger--- 
      if (event.getAction() == 1) {     
       GeoPoint p = mapView.getProjection().fromPixels(
        (int) event.getX(), 
        (int) event.getY()); 
      }        
      return false; 
     } 
     public boolean onTap(GeoPoint point, MapView mapView) 
     { 

      String msg = "Lat:: " + point.getLatitudeE6()/1E6 + " - " + 
         "Lon:: " + point.getLongitudeE6()/1E6; 
      Toast.makeText(mapView.getContext(), msg,1).show(); 
      return true; 
     } 
     @Override 
     public void onLocationChanged(Location loc) { 
      if (loc != null) {     
       Toast.makeText(mapView.getContext(), 
        "Location changed : Lat: " + loc.getLatitude() + 
        " Lng: " + loc.getLongitude(), 
        1).show(); 

       GeoPoint p = new GeoPoint(
         (int) (loc.getLatitude() * 1E6), 
         (int) (loc.getLongitude() * 1E6)); 
       mc.animateTo(p); 
       mc.setZoom(15);     
       mapView.invalidate();  
      } 
     } 
     @Override 
     public void onProviderDisabled(String provider) { 
      // TODO Auto-generated method stub 
      Toast.makeText(mapView.getContext(), "hee", 1).show(); 
     } 
     @Override 
     public void onProviderEnabled(String provider) { 
      // TODO Auto-generated method stub 
      Toast.makeText(mapView.getContext(), "hee", 1).show(); 
     } 
     @Override 
     public void onStatusChanged(String provider, int status, 
      Bundle extras) { 
      // TODO Auto-generated method stub 
      Toast.makeText(mapView.getContext(), "hee", 1).show(); 
     } 
    }  
} 
+0

を試してみてくださいそして、許可

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.INTERNET"></uses-permission> 

を追加している必要があります? – freshDroid

+0

出力は私が与えている固定座標に来ているが、私はユーザーの元の位置が欲しい、あなたは私に教えてください?????? – Ravi

+0

エミュレータでこれをやっていますか? – freshDroid

答えて

1

まず、あなたはヨール電流出力が何であるか、

Criteria criteria = new Criteria(); 
String bestProvider = locationManager.getBestProvider(criteria, true); 
Location location = locationManager.getLastKnownLocation(bestProvider); 
locationManager.requestLocationUpdates(bestProvider, 0, 0, new TestLocationListener()); 

代わりに、

lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);  
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,new MyLocationListener()); 
6

これは私のために働く...私がそれがあなたを助けることを願っています。それに印を付けるなら。

public class MyGoogleMapActivity extends MapActivity implements LocationListener { 
/** Called when the activity is first created. */ 
EditText  txted   = null; 
Button   btnSimple  = null; 
MapView   gMapView  = null; 
MapController mc    = null; 
Drawable  defaultMarker = null; 
GeoPoint  p    = null; 
double lattitude=18.5297 ; 
double longitude=73.8491 ; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    txted=(EditText)findViewById(R.id.id1); 
    String currentLocation="Lattitude :"+lattitude+" Longitude :"+longitude; 
    txted.setText(currentLocation); 

    gMapView=(MapView)findViewById(R.id.myGMap); 
    p= new GeoPoint((int)(lattitude * 1E6), (int)(longitude * 1E6)); 


    Canvas canvas=new Canvas(); 
    MyLocationOverlay locationOverlay=new MyLocationOverlay(); 

    List<Overlay> list=gMapView.getOverlays(); 
    list.add(locationOverlay); 

    ZoomControls zoomControls=(ZoomControls) gMapView.getZoomControls(); 
    zoomControls.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    gMapView.addView(zoomControls); 
    gMapView.displayZoomControls(true); 
    mc=gMapView.getController(); 
    mc.setCenter(p); 
    mc.animateTo(p); 
    mc.setZoom(14); 
    LocationManager locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this); 
} 

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

@Override 
public void onLocationChanged(Location location) { 
    if(location!=null){ 
     double lat=location.getLatitude(); 
     double lng=location.getLongitude(); 
     String currentLocation="Lattitude :"+lat+" Longitude :"+lng; 
      txted.setText(currentLocation); 
      mc.animateTo(p); 

    } 

} 

@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 

} 
protected class MyLocationOverlay extends com.google.android.maps.Overlay{ 

    @Override 
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, 
      long when) { 
     Paint paint=new Paint(); 
     super.draw(canvas, mapView, shadow, when); 
     Point myScreenCords= new Point(); 
     mapView.getProjection().toPixels(p, myScreenCords); 
     paint.setStrokeWidth(1); 
     paint.setARGB(255, 255, 255, 255); 
     paint.setStyle(Paint.Style.STROKE); 
     Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.marker); 
     canvas.drawBitmap(bmp, myScreenCords.x, myScreenCords.y, paint); 
     canvas.drawText("I m here...", myScreenCords.x,myScreenCords.y, paint); 
     return true; 

    } 

    @Override 
    public boolean onTouchEvent(MotionEvent e, MapView mapView) { 
     super.onTouchEvent(e, mapView); 
     if(e.getAction()==1){ 
      GeoPoint gp=gMapView.getProjection().fromPixels((int)e.getX(),(int)e.getY()); 
     } 
     return true; 
    } 


} 
+0

それは私のために働いていません。それは私の場所ではなく最初のジオポイントを表示するだけです。 – ghostrider

関連する問題