package com.yyxiang.MyMap1;import java.util.List;
import java.util.Locale;import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Address;
import android.location.Geocoder;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;public class MyMap2Activity extends MapActivity {
/** Called when the activity is first created. */
private Bitmap bitmap;
private EditText mEditText1;
private MapView mapView;
private Button myButton,myButton1, myButton2;
private RadioButton rbNormal,rbSatellite;
private MapController mapController;
private int level = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a);
mEditText1 = (EditText) findViewById(R.id.myEditText1);
myButton = (Button) findViewById(R.id.myButton);
mapView = (MapView) findViewById(R.id.myMapView); mapController = mapView.getController();
level = 15;
mapController.setZoom(level);
myButton.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
String fromAddress = mEditText1.getText().toString();
GeoPoint fromGeoPoint = getGeoPointByAddress(fromAddress);
// 创建Intent
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)
+ "&h1=cn") );
startActivity(intent);// 启动地图Activity
if (fromGeoPoint != null)
{
mapController.animateTo(fromGeoPoint);
List<Overlay> ols = mapView.getOverlays();
ols.clear();
ols.add(new ArrowOverLay(bitmap, fromGeoPoint));// 添加一个新的Overlay
}
else{
Toast.makeText(MyMap2Activity.this, "对不起,请输入地址!", Toast.LENGTH_LONG).show();
return;
}
}
});
// 将GeoPoint里的经纬度转换成需要的字符串
private String geoPointToString(GeoPoint gp)
{
String strReturnString = "";
try
{
// Location存在
if (gp != null)
{
double geoLatitude = (int) gp.getLatitudeE6() / 1E6;// 得到经纬度
double geoLongitude = (int) gp.getLongitudeE6() / 1E6;
strReturnString = String.valueOf(geoLatitude) + ","
+ String.valueOf(geoLongitude);
}
}
catch (Exception e)
{
e.printStackTrace();
}
return strReturnString;
}
// 通过地址得到GeoPoint
public GeoPoint getGeoPointByAddress(String address)
{
GeoPoint geoPoint = null;// 声明GeoPoint
try
{
if (address != "")
{
Geocoder mGeocoder = new Geocoder(this,Locale.getDefault());
List<Address> listaddress = mGeocoder.getFromLocationName(
address, 1);// 得到Address
if (listaddress!=null)
{
Address tempAddress = listaddress.get(0);// 获得第一个Address double mLatitude = tempAddress.getLatitude() * 1E6;// 计算经纬度
double mLongiyude = tempAddress.getLongitude() * 1E6; geoPoint = new GeoPoint((int) mLatitude, (int) mLongiyude);
} }
}
catch (Exception e)
{
e.printStackTrace();
}
return geoPoint;
} protected boolean isRouteDisplayed()
{
// TODO Auto-generated method stub
return false;
}
}
红色这段代码为什么通过地址得到GeoPoint
返回的是空值?应如何改动使其不为空?
import java.util.Locale;import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Address;
import android.location.Geocoder;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;public class MyMap2Activity extends MapActivity {
/** Called when the activity is first created. */
private Bitmap bitmap;
private EditText mEditText1;
private MapView mapView;
private Button myButton,myButton1, myButton2;
private RadioButton rbNormal,rbSatellite;
private MapController mapController;
private int level = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a);
mEditText1 = (EditText) findViewById(R.id.myEditText1);
myButton = (Button) findViewById(R.id.myButton);
mapView = (MapView) findViewById(R.id.myMapView); mapController = mapView.getController();
level = 15;
mapController.setZoom(level);
myButton.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
String fromAddress = mEditText1.getText().toString();
GeoPoint fromGeoPoint = getGeoPointByAddress(fromAddress);
// 创建Intent
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)
+ "&h1=cn") );
startActivity(intent);// 启动地图Activity
if (fromGeoPoint != null)
{
mapController.animateTo(fromGeoPoint);
List<Overlay> ols = mapView.getOverlays();
ols.clear();
ols.add(new ArrowOverLay(bitmap, fromGeoPoint));// 添加一个新的Overlay
}
else{
Toast.makeText(MyMap2Activity.this, "对不起,请输入地址!", Toast.LENGTH_LONG).show();
return;
}
}
});
// 将GeoPoint里的经纬度转换成需要的字符串
private String geoPointToString(GeoPoint gp)
{
String strReturnString = "";
try
{
// Location存在
if (gp != null)
{
double geoLatitude = (int) gp.getLatitudeE6() / 1E6;// 得到经纬度
double geoLongitude = (int) gp.getLongitudeE6() / 1E6;
strReturnString = String.valueOf(geoLatitude) + ","
+ String.valueOf(geoLongitude);
}
}
catch (Exception e)
{
e.printStackTrace();
}
return strReturnString;
}
// 通过地址得到GeoPoint
public GeoPoint getGeoPointByAddress(String address)
{
GeoPoint geoPoint = null;// 声明GeoPoint
try
{
if (address != "")
{
Geocoder mGeocoder = new Geocoder(this,Locale.getDefault());
List<Address> listaddress = mGeocoder.getFromLocationName(
address, 1);// 得到Address
if (listaddress!=null)
{
Address tempAddress = listaddress.get(0);// 获得第一个Address double mLatitude = tempAddress.getLatitude() * 1E6;// 计算经纬度
double mLongiyude = tempAddress.getLongitude() * 1E6; geoPoint = new GeoPoint((int) mLatitude, (int) mLongiyude);
} }
}
catch (Exception e)
{
e.printStackTrace();
}
return geoPoint;
} protected boolean isRouteDisplayed()
{
// TODO Auto-generated method stub
return false;
}
}
红色这段代码为什么通过地址得到GeoPoint
返回的是空值?应如何改动使其不为空?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货