1、在本节中,你将继续在前一节的基础上构造。对AndroidLBS活动的主要修改就是传递坐标到Google地图中。你将使用Google地图来显示用户的当前位置。在main.xml文件中的唯一修改指出就是为MpaView增加一个布局。在目前版本的Android SDK中,MapView被建立为一个类View。可能在将来的版本中MapView会相当于这个布局。
2、ut_height="wrap_content"/>
完成后的main.xml文件应当像这样:
7、ogle.android.maps.MapView;
import com.google.android.maps.Point;
import com.google.android.maps.MapController
Point包装将被用于保留point的值,它就是展示地图坐标的,而MapController将你的point置于地图中央。这两个包装在使用MapView时非常的关键。
现在准备增加建立地图并传递坐标的代码。首先,设置一个一个MapView,并且从main.xml文件中把它赋值到布局:
MapView myMap = (MapView) findViewById(R.i 8、d.myMap);
下一步,设置一个Point并且把从GPS检索的数值赋值给latPoint和IngPoint:
Point myLocation = new Point(latPoint.intValue(),lngPoint.intValue());
现在,可以创建MapController了,它将被用于移动Google地图来定位你定义的Point。从MapView使用getController()方法在定制的地图中建立一个控制器:
MapController myMapController = myMap.getController();
唯一剩下的工作就是使用控制器来移动地图 9、到你的位置(要让地图更容易辨认,把zoom设定为9):
myMapController.centerMapTo(myLocation, false);
myMapController.zoomTo(9);
你刚才所写的所有代码就是从活动中利用Google地图。完整的类应当像这样:
package android_programmers_guide.AndroidLBS;
import android.os.Bundle;
import android.location.LocationManager;
import android.view.View;
import andro 10、id.widget.TextView;
import android.content.Context;
import android.widget.Button;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Point;
import com.google.android.maps.MapController;
public class AndroidLBS extends MapActivity 11、 {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
final Button gpsButton = (Button) findViewById(R.id.gpsButton);
gpsButton.setOnClickListener(new Button.OnClickListener() {
public void 12、onClick(View v){
LoadProviders();
}});
}
public void LoadProviders(){
TextView latText = (TextView) findViewById(R.id.latText);
TextView lngText = (TextView) findViewById(R.id.lngText);
LocationManager myManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
Double latPoint 13、 =
myManager.getCurrentLocation("gps").getLatitude()*1E6;
Double lngPoint =
myManager.getCurrentLocation("gps").getLongitude()*1E6;
latText.setText(latPoint.toString());
lngText.setText(lngPoint.toString());
MapView myMap = (MapView) findViewById(R.id.myMap);
Point myLocation = new Point(latPoint.intValue(),lngPoint.intValue());
MapController myMapController = myMap.getController();
myMapController.centerMapTo(myLocation, false);
myMapController.zoomTo(9);
}
}
在模拟器中运行活动。活动应当打开一个空白的地图。点击“Where Am I”按钮,应当会看到地图聚焦并且放大到旧金山。看看下图就会知道地图会如何出现(略)。






