반응형
이번엔 앞의 소스에 지오코딩과 역 지오코딩을 추가하였다.
마찬가지로 구글 API 에 가면 많은 정보가 있다.
그리고 아이콘을 오버레이해서 추가하는 것까지 했다.(이부분은 do it mission때문에)
지오코딩은 내가 위치나 주소를 적고 검색을 하게되면 구글엔진을 통해 검색을 하고,
그쪽 좌표를 찾아 맵으로 보여준다.
이부분은 처음 실행때 GPS를 통해 위치를 찾는 부분이다.
찾은 좌표를 이용해 지도에 표시를 하고, 역지오코딩을 이용해 주소를 알려준다.
This is index.html file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | <!DOCTYPE html> <html> <head> <title>Geolocation</title> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> <meta charset="utf-8"> <style> html, body, #map-canvas { height: 100%; margin: 0px; padding: 0px } #panel { position: absolute; top: 5px; left: 50%; margin-left: -180px; z-index: 5; background-color: #fff; padding: 5px; border: 1px solid #999; } </style> <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true"></script> <script> var map; var pos; var marker; var starbuckslatlng; var kfclatlng; var geocoder; function initialize() { var mapOptions = { zoom: 15 }; var curLat; var curLng; geocoder = new google.maps.Geocoder(); map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions); if(navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position) { curLat=position.coords.latitude; curLng=position.coords.longitude; alert("My location is\nlatitude:"+curLat+"\nlongitude:"+curLng); pos = new google.maps.LatLng(curLat,curLng); marker = new google.maps.Marker({ map: map, //animation: google.maps.Animation.BOUNCE, position: pos }); map.setCenter(pos); reverseGeocode(curLat,curLng); }, function() { handleNoGeolocation(true); }); } else { handleNoGeolocation(false); } addItem(); } function reverseGeocode(relat,relng){ //for reverse Geocoding var regeocoder = new google.maps.Geocoder(); var relatlng=new google.maps.LatLng(relat,relng); var info = new google.maps.InfoWindow({ map: map, position: relatlng }); regeocoder.geocode({ 'latLng': relatlng}, function(results, status){ if(status == google.maps.GeocoderStatus.OK){ if(results[1]){ info.setContent(results[1].formatted_address); info.open(map,marker); }else{ alert("Geocoder failed due to:"+status); } } }); } function addItem(){ //for add Item(overlay) starbuckslatlng=new google.maps.LatLng(35.218,128.6); kfclatlng=new google.maps.LatLng(35.215,128.6); starbucksMap=new google.maps.Marker({ position:starbuckslatlng, icon:'starbucks.jpg', map:map }); kfcMap=new google.maps.Marker({ position:kfclatlng, icon:'kfc.jpg', map:map }); google.maps.event.addListener(starbucksMap,'click',function(){ alert("여기는 스타벅스입니다"); }); google.maps.event.addListener(kfcMap,'click',function(){ alert("여기는 kfc입니다"); }); } function handleNoGeolocation(errorFlag) { if (errorFlag) { var content = 'Error: The Geolocation service failed.'; } else { var content = 'Error: Your browser doesn\'t support geolocation.'; } var options = { map: map, position: new google.maps.LatLng(35, 125), content: content }; var infowindow = new google.maps.InfoWindow(options); map.setCenter(options.position); } function codeAddress() { //for Geocoding var address = document.getElementById('address').value; geocoder.geocode( { 'address': address}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { map.setCenter(results[0].geometry.location); var marker2 = new google.maps.Marker({ map: map, position: results[0].geometry.location }); var infowindow=new google.maps.InfoWindow({ map:map, position: results[0].geometry.location, content:address }); infowindow.open(map,marker2); } else { alert('Geocode was not successful for the following reason: ' + status); } }); } google.maps.event.addDomListener(window, 'load', initialize); </script> </head> <body> <div id="panel"> <input id="address" type="textbox" value="창원시청"> <input type="button" value="Go!!" onclick="codeAddress()"> </div> <div id="map-canvas"></div> </body> </html> | cs |
This is mainactivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | package com.answerofgod.mymapgeocode; import android.app.Activity; import android.os.Bundle; import android.webkit.GeolocationPermissions; import android.webkit.GeolocationPermissions.Callback; import android.webkit.WebChromeClient; import android.webkit.WebView; // for using geolocation method public class MainActivity extends Activity implements GeolocationPermissions.Callback { WebView mapview; //just webview. String url="file:///android_asset/www/index.html"; //local html file protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mapview=(WebView) findViewById(R.id.mapview); //casting webview mapview.getSettings().setJavaScriptEnabled(true); //webview options mapview.getSettings().setGeolocationEnabled(true); // mapview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); // Geoclient geoclient=new Geoclient(); //casting class mapview.setWebChromeClient(geoclient); //set webchromeclient for permission String origin=""; geoclient.onGeolocationPermissionsShowPrompt(origin,this); //for permission mapview.loadUrl(url); } public void invoke(String origin, boolean allow, boolean retain) { } class Geoclient extends WebChromeClient{ //for display mylocation @Override public void onGeolocationPermissionsShowPrompt(String origin,Callback callback){ super.onGeolocationPermissionsShowPrompt(origin, callback); callback.invoke(origin,true,false); } } } | cs |
반응형
'Study > Android' 카테고리의 다른 글
MyLocation&MyPath (정확도 증가버전) (0) | 2015.02.11 |
---|---|
Do it 안드로이드 앱 프로그래밍 미션 20 (4) | 2015.02.11 |
구글맵 V3이용한 지오로케이션(Google Maps JavaScript V3) (0) | 2015.02.11 |
간단한 구글맵 (using google static map) (0) | 2015.02.11 |
Do it 안드로이드 앱 프로그래밍 미션 18 (0) | 2015.02.11 |