본문 바로가기

안드로이드

Mapview 현재위치 표시 , Mapview 터치시 마커표시

반응형

//위치기반 앱에 가장 큰 문제는 현재위치를 정확하게 표시하기 어렵다는 점이다.

앱은 주로 실내에서  사용하고 실내에서는 gps가 잡히지 않기 때문에 네트워크로 위치를 잡아야 되는데

wifi나 3g 망에서는 무선공유기 위치에 따라 자기 위치가 설정되는 경우가 많아서 위치가 정확하지가 않다.

//여튼 현재위치를 직접 세팅할수 있도록 맵뷰터치시 위치를 지정할수 있는 예제를 만들어 보았다.

//시작할때 현재위치를 보여주고 지도보기를 누르면 지도가 나오고, 맵뷰를 터치할때마다 터치된 위치에 마커를 보여주고 위도 경도를 찍어주는 예제이다. 


=========================================================================================================

MainActivity .java



public class MainActivity extends MapActivity {

ProgressDialog loagindDialog;

MapView mapView;

double nowLat;

double nowLongi;


@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.activity_main);

mapView = (MapView) findViewById(R.id.mapview);

mapView.setVisibility(View.GONE);

getLocat();//현재위치 받아와서 주소로 변환하여 출력 

Button mapShowbtn = (Button) findViewById(R.id.mapshow);

mapShowbtn.setOnClickListener(clickListener);

}


Button.OnClickListener clickListener = new Button.OnClickListener() {

public void onClick(View v) {

switch (v.getId()) {

case R.id.mapshow:

showMap();//지도보여주고 오버레이 세팅

}

}

};


private void showMap() {

mapView.setVisibility(View.VISIBLE);


Drawable marker = getResources().getDrawable(R.drawable.map_icon);

marker.setBounds(0, 0, marker.getIntrinsicWidth(),marker.getIntrinsicHeight());


InterestringLocatinos funPlaces = new InterestringLocatinos(marker);

mapView.getOverlays().add(funPlaces);//현재위치에 마커 표시


ClickReceiver clickRecvt = new ClickReceiver(this, funPlaces);//터치했을때 오버레이 설정

mapView.getOverlays().add(clickRecvt);

}


public class ClickReceiver extends Overlay {

private Context context;

InterestringLocatinos temp;


public ClickReceiver(Context _context, InterestringLocatinos temp_) {

context = _context;

temp = temp_;

}


@Override

public boolean onTap(GeoPoint p, MapView mapView) {

GeoPoint b = new GeoPoint((int) p.getLatitudeE6(),(int) p.getLongitudeE6());

mapView.getController().animateTo(b);

temp.clear();//터치시 이전 오버레이 지워준다

temp.addOverlay(new OverlayItem(b, "test", "test"));


myAddress.setText("위치 : " + (double)p.getLatitudeE6() / 1000000 + "  ,"+ (double)p.getLongitudeE6() / 1000000);


return true;

}

}


class InterestringLocatinos extends ItemizedOverlay<OverlayItem> {


private List<OverlayItem> locations = new ArrayList<OverlayItem>();

private Drawable marker;


public InterestringLocatinos(Drawable marker) {

super(marker);

this.marker = marker;


int latitude = (int) (nowLat * 1E6);

int longitude = (int) (nowLongi * 1E6);


GeoPoint gp = new GeoPoint(latitude, longitude);

mapView.getController().setCenter(gp);

mapView.getController().setZoom(19);

locations.add(new OverlayItem(gp, "test", "test"));


populate();

}


@Override

public void draw(Canvas canvas, MapView mapView, boolean shadow) {

super.draw(canvas, mapView, shadow);

boundCenterBottom(marker);

}


@Override

protected OverlayItem createItem(int i) {

return locations.get(i);

}


@Override

public int size() {

return locations.size();

}


public void addOverlay(OverlayItem overlay) {

locations.add(overlay);

setLastFocusedIndex(-1);

populate();

}


public void clear() {

locations.clear();

mapView.removeAllViews();

setLastFocusedIndex(-1);

populate();

}


}


public void getLocat() {//현재위치 받아옮

loagindDialog = ProgressDialog.show(MainActivity.this, "현재위치 설정 중..",

"Please wait..", true, false);

new Thread(new Runnable() {

@Override

public void run() {

runOnUiThread(new Runnable() {

@Override

public void run() {

MapView mapView = (MapView) findViewById(R.id.mapview);


final MyLocationOverlay myLocationOverlay = new MyLocationOverlay(

MainActivity.this, mapView);

myLocationOverlay.enableMyLocation();


if (myLocationOverlay.isMyLocationEnabled()) {

myLocationOverlay.runOnFirstFix(new Runnable() {

public void run() {

nowLat = (double) myLocationOverlay

.getMyLocation().getLatitudeE6() / 1000000f;

nowLongi = (double) myLocationOverlay

.getMyLocation().getLongitudeE6() / 1000000f;

mHandler.sendEmptyMessageDelayed(0, 10);//현재위치 받아온 후 주소로 변환

}

});

}


}

});

}

}).start();

}


Handler mHandler = new Handler() {//현재위치 받아온 후 주소로 변환

public void handleMessage(Message msg) {

loagindDialog.dismiss();

String address = getAddress(nowLat, nowLongi);

myAddress = (TextView) findViewById(R.id.mylocation);

myAddress.setText(address);

}


};


TextView myAddress;


public String getAddress(double lat, double longi) {

String result = null;


Geocoder geoCoder = new Geocoder(MainActivity.this);


List<Address> addresses = null;

try {

addresses = geoCoder.getFromLocation(lat, longi, 5);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

if (addresses != null && addresses.size() > 0) {

Address mAddress = addresses.get(0);

result = mAddress.getAddressLine(0);

} else {

result = "현재위치를 표시할 수 없습니다";

}

return result;

}


@Override

protected boolean isRouteDisplayed() {

// TODO Auto-generated method stub

return false;

}


@Override

public void onBackPressed() {

finish();

System.exit(0);

super.onBackPressed();

}


}


=========================================================================================================

activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="0dip"

        android:layout_weight="1"

        android:orientation="horizontal" >

        <LinearLayout

            android:layout_width="0dip"

            android:layout_weight="2"   android:orientation="vertical"

            android:layout_height="match_parent" >

              <TextView

            android:layout_width="match_parent"

            android:layout_weight="2" android:text="위도/경도" android:gravity="center"

            android:layout_height="0dip" />

                <TextView

            android:id="@+id/mylocation"

            android:layout_width="match_parent"

            android:layout_weight="2" android:gravity="center"

            android:layout_height="0dip" />

        </LinearLayout>

        

        <Button

            android:id="@+id/mapshow"

            android:layout_width="0dip"

            android:layout_weight="1"

            android:layout_height="match_parent"

            android:text="지도보기" />

    </LinearLayout>


    <LinearLayout

        android:layout_width="match_parent"

       android:layout_height="0dip"

        android:layout_weight="4">


        <com.google.android.maps.MapView

            android:id="@+id/mapview"

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:apiKey="api키를 입력하세요"

            android:clickable="true" />

    </LinearLayout>


</LinearLayout>

========================================================================================================



final MyLocationOverlay myLocationOverlay = new MyLocationOverlay(MainActivity.this, mapView);

myLocationOverlay.enableMyLocation();

//현재 위치를 MyLocationOverlay 이용해서 받아와 보았다. 역시나 오차가 있다. 이방법을 많이 사용한다고 하는데 확인할 길은 없다. 제일 좋은 방법은 gps켜고 밖에나가서 gps프로바이더를 이용해서 현재위치를 잡으면 정확하게 나온다............




반응형