?
Color.xml文件内容如下:
<?xml version="1.0" encoding="utf-8"?> <resources> ? <drawable name="darkgray">#808080</drawable> ? <drawable name="white">#FFFFFF</drawable> ? <drawable name="blue">#0000FF</drawable> </resources> |
String.xml文件内容如下:
<?xml version="1.0" encoding="utf-8"?> <resources> ? <string name="hello">Hello World, QueryMapLocation!</string> ? <string name="app_name">QueryMapLocation</string> ? <string name="str_longitude">经度(Longitude):</string> ? <string name="str_latitude">纬度(Latitude):</string> ? <string name="str_button1">查询</string> ? <string name="str_button2">+</string> ? <string name="str_button3">-</string> </resources> |
QueryMapLocation.java文件的内容如下:
package org.yan; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapActivity; import com.google.android.maps.MapController; import com.google.android.maps.MapView; public class QueryMapLocation extends MapActivity { ??????? ? private MapController mMapController01; ??????? ? private MapView mMapView01; ??????? ? private Button mButton01,mButton02,mButton03; ??????? ? private EditText mEditText01; ??????? ? private EditText mEditText02; ??????? ? private int intZoomLevel=0; ??????? ? private double dLat=25.0402555; ??????? ? private double dLng=121.512377; ??????? ? @Override ??????? ? protected void onCreate(Bundle icicle) ??????? ? { ??????? ??? super.onCreate(icicle); ??????? ??? setContentView(R.layout.main); ??????? ??? mMapView01 = (MapView)findViewById(R.id.myMapView1); ??????? ??? mMapController01 = mMapView01.getController();? ??????? ??? mMapView01.setSatellite(false); ??????? ??? mMapView01.setStreetView(true); ??????? ??? intZoomLevel = 10; ??????? ??? mMapController01.setZoom(intZoomLevel); ??????? ??? refreshMapView(); ??????? ??? mEditText01 = (EditText)findViewById(R.id.myEdit1); ??????? ??? mEditText02 = (EditText)findViewById(R.id.myEdit2); ??????? ??? mButton01 = (Button)findViewById(R.id.myButton1); ??????? ??? mButton01.setOnClickListener(new Button.OnClickListener() ??????? ??? { ??????? ????? @Override ??????? ????? public void onClick(View v) ??????? ????? { ??????? ?????? ??????? ??????? if(mEditText01.getText().toString().equals("")|| ??????? ?????????? mEditText02.getText().toString().equals("")) ??????? ??????? { ??????? ????????? showDialog("不能为空"); ??????? ??????? } ??????? ??????? else ??????? ??????? { ??????? ????????? dLng=Double.parseDouble(mEditText01.getText().toString()); ??????? ????????? dLat=Double.parseDouble(mEditText02.getText().toString()); ??????? ????????? refreshMapView(); ??????? ??????? } ??????? ????? } ??????? ??? }); ? ??????? ??? mButton02 = (Button)findViewById(R.id.myButton2); ??????? ??? mButton02.setOnClickListener(new Button.OnClickListener() ??????? ??? { ??????? ????? @Override ??????? ????? public void onClick(View v) ??????? ????? { ??????? ??????? intZoomLevel++; ??????? ??????? if(intZoomLevel>mMapView01.getMaxZoomLevel()) ??????? ??????? { ??????? ????????? intZoomLevel = mMapView01.getMaxZoomLevel(); ??????? ??????? } ??????? ??????? mMapController01.setZoom(intZoomLevel); ??????? ????? } ??????? ??? });? ??????? ??? mButton03 = (Button)findViewById(R.id.myButton3); ??????? ??? mButton03.setOnClickListener(new Button.OnClickListener() ??????? ??? { ??????? ????? @Override ??????? ????? public void onClick(View v) ??????? ????? { ??????? ??????? intZoomLevel--; ??????? ??????? if(intZoomLevel<1) ??????? ??????? { ??????? ????????? intZoomLevel = 1; ??????? ??????? } ??????? ??????? mMapController01.setZoom(intZoomLevel); ??????? ????? } ??????? ??? }); ??????? ? }? ??????? ? public void refreshMapView() ??????? ? { ??????? ??? GeoPoint p = new GeoPoint((int)(dLat* 1E6), (int)(dLng* 1E6)); ??????? ??? mMapView01.displayZoomControls(true); ??????? ?? ??????? ??? mMapController01.animateTo(p); ??????? ??? mMapController01.setZoom(intZoomLevel); ??????? ? } ??????? ?? ??????? ? @Override ??????? ? protected boolean isRouteDisplayed() ??????? ? { ??????? ??? return false; ??????? ? } ??????? ? private void showDialog(String mess){ ??????? ??? new AlertDialog.Builder(QueryMapLocation.this).setTitle("Message") ??????? ??? .setMessage(mess) ??????? ??? .setNegativeButton("经度纬度请补全", new DialogInterface.OnClickListener() ??????? ??? { ??????? ????? public void onClick(DialogInterface dialog, int which) ??????? ????? { ??????? ????? } ??????? ??? }) ??????? ??? .show(); ??????? ? } ??????? } |
?
还有一个重要的文件AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" ????? package="org.yan" ????? android:versionCode="1" ????? android:versionName="1.0"> ??? <application android:icon="@drawable/icon" android:label="@string/app_name"> ??????? <uses-library? android:name="com.google.android.maps" /> ??????? <activity android:name=".QueryMapLocation" ????????????????? android:label="@string/app_name"> ??????????? <intent-filter> ??????????????? <action android:name="android.intent.action.MAIN" /> ??????????????? <category android:name="android.intent.category.LAUNCHER" /> ??????????? </intent-filter> ??????? </activity> ??? </application> <uses-sdk android:minSdkVersion="4" /> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> </manifest> |
(3)运行该Android项目,即可见到如下界面
输入经度纬度,即可定位到地图上