<?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"
android:weightSum="1">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/myEditText1"
android:layout_width="130px"
android:layout_height="wrap_content"
android:text="@string/myEditText1"
/>
<TextView
android:gravity="center_vertical"
android:layout_width="55px"
android:layout_height="wrap_content"
android:text="@string/to"
/>
<EditText
android:id="@+id/myEditText2"
android:layout_width="130px"
android:layout_height="wrap_content"
android:text="@string/myEditText2"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/fromButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fromButton"
/>
<Button
android:id="@+id/toButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/toButton"
/>
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/myButton"
/>
</LinearLayout>
<com.google.android.maps.MapView
android:id="@+id/myMapView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:apiKey="0KHHUtPvITPiOsko5zl-OwyDkw9ZvxNKW5Kyb-w" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
string.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, GoogleMapsActivity!</string>
<string name="app_name">GoogleMaps</string>
<string name="myButton">查询路径</string>
<string name="to">到</string>
<string name="myEditText1">北京*</string>
<string name="myEditText2">前门</string>
<string name="fromButton">显示终点</string>
<string name="toButton">显示终点</string>
</resources>
Activity代码:
package com.dk.cn;
//import wyf.ytl.R;
import java.io.IOException;
import java.util.List;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
public class GoogleMapsActivity extends Activity implements OnClickListener {
EditText myEditText1;
EditText myEditText2;
MapView myMapView;
Button myButton;
Button fromButton;
Button toButton;
MapController myMapController;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myEditText1 = (EditText) this.findViewById(R.id.myEditText1);
myEditText2 = (EditText) this.findViewById(R.id.myEditText2);
myButton = (Button) this.findViewById(R.id.myButton);
fromButton = (Button) this.findViewById(R.id.fromButton);
toButton = (Button) this.findViewById(R.id.toButton);
myButton.setOnClickListener(this);
toButton.setOnClickListener( this);
fromButton.setOnClickListener(this);
myMapView = (MapView) this.findViewById(R.id.myMapView);
myMapController = myMapView.getController();
myMapController.setZoom(18);
}
@Override
public void onClick(View v) {
if(v==myButton){
String fromAddress=myEditText1.getText().toString();
String toAddress=myEditText2.getText().toString();
GeoPoint fromGeoPoint = getGeoPointByAddress(fromAddress);
GeoPoint toGeoPoint = getGeoPointByAddress(toAddress);
Intent intent =new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://maps.google.com/maps?f=d&saddr="+ geoPointToString(fromGeoPoint) +"&daddr="+geoPointToString(toGeoPoint)+"&h1=cn")
);
startActivity(intent);
}else if(v==fromButton){
String fromAddress =myEditText1.getText().toString();
GeoPoint fromGeoPoint =getGeoPointByAddress(fromAddress);
if(fromGeoPoint!=null){
myMapController.animateTo(fromGeoPoint);
}else if(v==toButton){
String toAddress =myEditText2.getText().toString();
GeoPoint toGeoPoint =getGeoPointByAddress(toAddress);
if(toGeoPoint !=null){
myMapController.animateTo(toGeoPoint);
}
}
}
// TODO Auto-generated method stub
}
private String geoPointToString(GeoPoint gp) {
String result=",";
try{
if(gp!=null){
double geoLatitude=(int)gp.getLatitudeE6()/1E6;
double geoLongitude=(int)gp.getLongitudeE6()/1E6;
result=String.valueOf(geoLatitude)+","+String.valueOf(geoLongitude);
}
}catch(Exception e){
e.printStackTrace();
}
// TODO Auto-generated method stub
return null;
}
private GeoPoint getGeoPointByAddress(String address) {
GeoPoint geoPoint =null;
if(address !=""){
Geocoder myGeocoder =new Geocoder(this);
List<Address>addressList=null;
try{
addressList=myGeocoder.getFromLocationName(address, 1);
if(!addressList.isEmpty()){
Address tempAddress =addressList.get(0);
double myLatitude=tempAddress.getLongitude()*1E6;
double myLongitude =tempAddress.getLongitude()*1E6;
geoPoint =new GeoPoint((int)myLatitude,(int)myLongitude);
}
}catch(IOException e){
e.printStackTrace();
}
}
// TODO Auto-generated method stub
return geoPoint;
}
}
出错的问题在 setContentView(R.layout.main); 是程序运行后从日志找到的错误。目前还没解决 求助高手。
权限已加
介绍一下R.LAYOUT.MAIN出错的几种原因就OK了。谢谢大家!
5 个解决方案
#1
问题已解决 原来是继承子类 MapActivity
#2
既然解决。板凳,接分。
#3
lz你真厉害,,
#4
自娱自乐
#5
android 处女贴给你了。
#1
问题已解决 原来是继承子类 MapActivity
#2
既然解决。板凳,接分。
#3
lz你真厉害,,
#4
自娱自乐
#5
android 处女贴给你了。