按照《深入浅出google android》上第八章 第一节的讲解,把程序写好,运行后,总是显示“无法获取位置信息”。
后来才知道,模拟器运行起来后, 要点下emnulate control窗口中的send按钮,再运行程序,程序才能显示出经纬度。
我是分割线
搞了半天,不知道怎么上传该项目。贴下源代码吧,
主文件除了显示当前位置经纬度,加了一个地图的显示。
package com.tudou2;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.tudou2.R;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;
public class tudou2 extends MapActivity {
LinearLayout linearLayout;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationManager;
String serviceName = Context.LOCATION_SERVICE;
locationManager=(LocationManager)getSystemService(serviceName);
String provider=LocationManager.GPS_PROVIDER;
Location location=locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
}
private void updateWithNewLocation(Location location){
String latLongString;
TextView myLocationText;
myLocationText =(TextView)findViewById(R.id.text);
if(location!=null){
double lat= location.getLatitude();
double lng= location.getLongitude();
latLongString= "纬度:"+lat+"/n经度:"+lng;
}else{
latLongString="无法获取地理信息";
}
myLocationText.setText("您当前的位置是:"+latLongString);
}
@Overrideprotected
boolean isRouteDisplayed()
{// TODO Auto-generated method stubreturn false;
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:id="@+id/text" />
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="0kpKXxsZVPeXxlhF6AkW_lCaPKd23aVrq5D7fWA"/>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tudou2"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-library android:name="com.google.android.maps" /> <!--这句可能必须放在activity之前-->
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".tudou2" 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>
</manifest>