2012-05-01 3 views
0

안드로이드 프로그래밍에 익숙하지 않습니다. 두 개의 EditText 소스와 대상에서 위치를 가져 와서 아래지도에 표시하고 싶습니다. 버튼의 클릭에 글고에서지도에 표시 한 마커 :안드로이드에서 소스와 목적지에 대한 맵에 마커 두 개를 추가하는 방법

package bijoy.happy; 

import java.util.List; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapView; 
import com.google.android.maps.Overlay; 

//import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Point; 
import android.location.Address; 
import android.location.Geocoder; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class SearchByAddressActivity extends MapActivity { 
    MapView mapView; 
    Geocoder geocoder; 
    GeoPoint p,qt,pt; 

    class MapOverlay extends com.google.android.maps.Overlay 
{ 
    @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.pin);    
     canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);   
     return true; 
    } 
}//end of class 


    @Override 
/** Called when the activity is first created. */ 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    mapView = (MapView) findViewById(R.id.geoMap); 
    mapView.setBuiltInZoomControls(true); 
    mapView.setClickable(true); 


    // map starting point 
    int lat = (int) (51.678383 * 1000000); 
    int lng = (int) (19.334822 * 1000000); 
    pt = new GeoPoint(lat, lng); 


    mapView.getController().setZoom(8); 

    mapView.getController().setCenter(pt); 

    Button geoBtn = (Button) findViewById(R.id.geocodeBtn); 

    geocoder = new Geocoder(this);  


    geoBtn.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      try { 
       EditText locale = (EditText) findViewById(R.id.location); 
       String locationName = locale.getText().toString(); 

       List<Address> addressList = geocoder.getFromLocationName( 
         locationName, 5); 
       if (addressList != null && addressList.size() > 0) { 
        int lat = (int) (addressList.get(0).getLatitude() * 1e6); 
        int lng = (int) (addressList.get(0).getLongitude() * 1e6); 

        TextView pozycja = (TextView) findViewById(R.id.position); 
        pozycja.setText("lat: " 
          + addressList.get(0).getLatitude() + " lng: " 
          + addressList.get(0).getLongitude()); 

        p = new GeoPoint(lat, lng); 
        mapView.getController().setZoom(18); 
        mapView.getController().setCenter(p); 

        //---Add a location marker--- 
        MapOverlay mapOverlay = new MapOverlay(); 
        List<Overlay> listOfOverlays = mapView.getOverlays(); 
        listOfOverlays.clear(); 
        listOfOverlays.add(mapOverlay); 


        mapView.invalidate(); 

       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 

} 


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

XML 코드가 하나 더있을 것이다 :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 



      <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="100" > 

     <EditText 
      android:id="@+id/location" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:hint="Type Your Address here" 
      android:layout_weight="20" 
      > 
     </EditText> 

     <Button 
      android:id="@+id/geocodeBtn" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Search" 
      android:layout_weight="80" 
       /> 
    </LinearLayout> 

     <TextView 
      android:id="@+id/position" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Latitude/Longitude..." /> 

       <com.google.android.maps.MapView 
     android:id="@+id/geoMap" android:layout_width="fill_parent" 
     android:layout_height="fill_parent" android:clickable="true" 
     android:apiKey="0I0jOn_3OgbSahP7YkXmjc3d6fn2Rwrdepi0noQ" /> 

     </LinearLayout> 

이 클래스의 코드는 다음과 같습니다

내 XML이다 EditText와 버튼을 눌러 검색하십시오. 도와주세요.!

답변

0

다음 link은 도움을 얻을 수 있습니다.

+0

고마워요! 그 일을 :-) .. – Zerosleep

관련 문제