Android+Google Map API v3 Geocoding(地址轉經緯度度
package wells.example.googlemapexample;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final String MAP_URL = "file:///android_asset/googleMap.html";
private WebView webView;
private EditText addressText;
private Button submit;
private boolean webviewReady = false;
@Override
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.googlemaps);
addressText = (EditText) findViewById(R.id.addressText); getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (webviewReady) {
//webView.loadUrl("javascript:resizeMap("+ (webView.getHeight()) + ")");
webView.loadUrl("javascript:codeAddress('"+ addressText.getText() + "')");
//Toast.makeText(v.getContext(), "javascript:codeAddress('"+ addressText.getText() + "')", Toast.LENGTH_LONG).show();
}
}
});
setupWebView();//設定webview
}
/** Sets up the WebView object and loads the URL of the page **/
private void setupWebView(){
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
//Wait for the page to load then send the location information
webView.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url)
{
//webView.loadUrl(centerURL);
webviewReady = true;//webview已經載入完畢
webView.loadUrl("javascript:resizeMap("+ (webView.getHeight()) + ")");
}
});
webView.loadUrl(MAP_URL);
}
}
[googlemap.xml]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true" >
<EditText
android:id="@+id/addressText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_toRightOf="@+id/textView2"
android:clickable="false"
android:ems="10"
android:inputType="text"
android:text="台中市沙鹿區中棲路34號" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="地址"
android:textSize="20dp" />
</RelativeLayout>
<Button
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="false"
android:text="Goto" />
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusableInTouchMode="true" />
</LinearLayout>
[googleMap.html]
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Google Maps JavaScript API v3 Example: Marker Animations</title>
<link href="/maps/documentation/javascript/examples/default.css" rel="stylesheet">
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script>
var centerPoint = new google.maps.LatLng(24.15622, 120.68124); //中國醫藥大學
var marker;
var map;
var geocoder;
var image = 'taxi-icon.png';//image變數指向計程車圖示檔案,圖示檔案請放在相同目錄
function mark(lat, log){//標註座標函式
var m = new google.maps.LatLng(lat, log);
marker = new google.maps.Marker({
map:map,
draggable:true,
position: m,
icon: image //指定標示圖案為image
});
}
function centerAt(latitude, longitude){
myLatlng = new google.maps.LatLng(latitude,longitude);
map.panTo(myLatlng);
}
function resizeMap(size){//
var mid = document.getElementById('map_canvas');
mid.style.height = size + 'px';
if(map != null) {
google.maps.event.trigger(map, 'resize');
}
}
function codeAddress(address){
geocoder.geocode({'address': address },function(results, status){
if(status == google.maps.GeocoderStatus.OK){
map.setCenter(results[0].geometry.location);
var marker =new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
}else{
alert("Geocode was not successful for the following reason: "+ status);
}
});
}
function initialize() {
geocoder =new google.maps.Geocoder();
var mapOptions = {
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: centerPoint
};
map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width: device-width; height: 460px;">map div</div>
</body>
</html>
Comments