1.AndroidManifest.xml
<?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="Address2Map"
android:label="@string/app_name" >
<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.addressmap.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">"
<EditText
android:id="@+id/etAddr"
android:layout_width="240sp"
android:text="台北市中正區公園路30號"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnAddr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show" />
</LinearLayout>
<fragment
android:id="@+id/addrmap"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</LinearLayout>
3.Address2Map.java
import java.io.IOException;
import java.util.List;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.maps.GeoPoint;
import android.app.Activity;
import android.location.Address;
import android.location.Geocoder;
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 Address2Map extends Activity {
GeoPoint gp;
Button bt;
EditText et;
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addressmap);
bt = (Button) findViewById(R.id.btnAddr);
et = (EditText) findViewById(R.id.etAddr);
map = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.addrmap)).getMap();
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
getGPFromAddress(et.getText().toString());
LatLng position = null;
try {
position = new LatLng(gp.getLatitudeE6() / 1E6, gp
.getLongitudeE6() / 1E6);
} catch (Exception ex) {
Log.e("Error Line 49", "position error");
return;
}
if (map != null) {
Marker main = map.addMarker(new MarkerOptions()
.position(position)
.title("MainPOS")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
// Move the camera instantly to TaipeiMainStation with a
// zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(position,
20));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000,
null);
}
}
});
}
public void getGPFromAddress(String addr) {
if (!addr.equals("")) {
Geocoder geocoder = new Geocoder(this);
List<Address> addresses = null;
Address address = null;
try {
addresses = geocoder.getFromLocationName(addr, 1);
} catch (IOException e) {
Log.e("Error 78", "Error in geocoder");
return;
}
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();
}
}
}
沒有留言:
張貼留言