<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gmapv2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="AddressRoute"
android:label="路徑地圖" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyC5wZD4vNgP2Qa2jx7Dgh1aAtEN_jy9JJk" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<uses-library android:name="com.google.android.maps" />
</application>
</manifest>
2.addressroute.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="出發地" />
<EditText android:id="@+id/etSourceAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="目的地" />
<EditText android:id="@+id/etDestAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="台北市陽明山" />
<Button android:id="@+id/btnRoute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="路徑圖" />
</LinearLayout>
import java.io.IOException;
import java.util.List;
import com.google.android.maps.GeoPoint;
import android.app.Activity;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class AddressRoute extends Activity {
GeoPoint gp;
Button bt;
EditText etSource, etDest;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addressroute);
bt = (Button) findViewById(R.id.btnRoute);
etSource = (EditText) findViewById(R.id.etSourceAddress);
etDest = (EditText) findViewById(R.id.etDestAddress);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
try {
GeoPoint myGP = null;
GeoPoint destGP = null;
myGP = getGPFromAddress(etSource.getText().toString());
destGP = getGPFromAddress(etDest.getText().toString());
route(myGP, destGP);
} catch (Exception io) {
Toast.makeText(AddressRoute.this, "Please enter text",
Toast.LENGTH_LONG).show();
}
}
});
}
public GeoPoint getGPFromAddress(String addr) {
gp = null;
if (!addr.equals("")) {
Geocoder geocoder = new Geocoder(this);
List<Address> addresses = null;
Address address = null;
try {
addresses = geocoder.getFromLocationName(addr, 1);
} catch (IOException e) {
e.printStackTrace();
}
if (addresses == null || addresses.isEmpty()) {
Toast.makeText(this, "找不到該地址", Toast.LENGTH_SHORT).show();
} else {
address = addresses.get(0);
double geoLatitude = address.getLatitude() * 1E6;
double geoLongitude = address.getLongitude() * 1E6;
Log.e("Location",
address.getLatitude() + ":" + address.getLongitude());
gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);
}
} else {
Toast.makeText(this, "未輸入地址", Toast.LENGTH_SHORT).show();
}
return gp;
}
// 開啟Google地圖伺服器的導航功能
public void route(GeoPoint fromGP, GeoPoint destGP) {
String fromGPStr = String.valueOf(fromGP.getLatitudeE6() / 1E6) + ","
+ String.valueOf(fromGP.getLongitudeE6() / 1E6);
String destGPStr = String.valueOf(destGP.getLatitudeE6() / 1E6) + ","
+ String.valueOf(destGP.getLongitudeE6() / 1E6);
Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr="
+ fromGPStr + "&daddr=" + destGPStr);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setData(uri);
startActivity(intent);
}
}
沒有留言:
張貼留言