我想在个人资料视图的半屏幕上显示Google地图。在我的onCreate
方法中,我创建了AsyncTask
的新实例,以通过JSON从Google服务器检索纬度和经度。收到结果后,我打电话给:
GoogMapHandler gmh = new GoogMapHandler(fragmentManager, applicationContext);
gmh.initilizeMap(coord, "Username", "addressInfo");
这是我的GoogMapHandler
课程:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.content.Context;
import android.widget.Toast;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import com.google.android.gms.internal.fm;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class GoogMapHandler {
// Google Map
private GoogleMap googleMap;
private android.support.v4.app.FragmentManager fragmentManager;
private Context applicationContext;
public GoogMapHandler(FragmentManager fmanager, Context applicationContext) {
this.fragmentManager = fmanager;
this.applicationContext = applicationContext;
}
public void initilizeMap(LatLng coord, String username, String addrInfo) {
if (googleMap == null) {
Fragment fragment = this.fragmentManager.findFragmentById(R.id.map);
FragmentTransaction fragTransaction = this.fragmentManager
.beginTransaction();
fragTransaction.detach(fragment);
SupportMapFragment supportmapfragment = (SupportMapFragment) fragment;
googleMap = supportmapfragment.getMap();
if (googleMap == null) {
Toast.makeText(applicationContext,
"Sorry! Unable to create map.", Toast.LENGTH_SHORT)
.show();
} else {
Marker loc = googleMap.addMarker(new MarkerOptions()
.position(coord).title("User: " + username)
.snippet(addrInfo));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(coord,
15));
fragTransaction.attach(fragment);
fragTransaction.commit();
}
}
}
}
现在的问题是,我看不到地图,虽然没有错误。由于View已经创建,我试图“重绘”片段以显示地图,但它不起作用。
我怎样才能做到这一点?
非常感谢任何帮助。
修改
这是我的ProfileActivity
。根据建议,我使用onResume
创建了AsyncTask
。
public class ProfileActivity extends BaseActivity {
private String username, address, postalCode, country;
@Override
protected void onResume() {
Log.i("URL",
"http://maps.googleapis.com/maps/api/geocode/json?address="
+ address.replaceAll("\\s+", "+") + ",+" + postalCode
+ ",+" + country + "&sensor=false");
new LocationTask(getSupportFragmentManager(),
getApplicationContext())
.execute("http://maps.googleapis.com/maps/api/geocode/json?address="
+ address.replaceAll("\\s+", "+")
+ ",+"
+ postalCode
+ ",+" + country + "&sensor=false");
super.onResume();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
Intent intent = getIntent();
username = intent.getStringExtra(SearchResultsActivity.USERNAME);
if (username != null) {
setTitle("User: " + username);
}
address = intent.getStringExtra(SearchResultsActivity.ADDRESS);
postalCode = intent
.getStringExtra(SearchResultsActivity.POSTALCODE);
country = intent.getStringExtra(SearchResultsActivity.COUNTRY);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(username);
setContentView(textView);
}
}
个人资料布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/map_frame"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.2" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="fill_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</FrameLayout>
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
位置任务:
public class LocationTask extends
AsyncTask<String, String, StringBuilder> {
private EditText address = null;
private EditText postalCode = null;
private EditText country = null;
private Context applicationContext;
private FragmentManager fragmentManager;
private LatLng coord;
public LocationTask(FragmentManager fmanager,
Context applicationContext) {
this.fragmentManager = fmanager;
this.applicationContext = applicationContext;
}
@Override
protected StringBuilder doInBackground(String... params) {
HttpGet httpGet = new HttpGet(params[0]);
HttpClient client = new DefaultHttpClient();
HttpResponse response;
try {
response = client.execute(httpGet);
StringBuilder stringBuilder = new StringBuilder();
try {
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(StringBuilder result) {
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(result.toString());
} catch (JSONException e) {
e.printStackTrace();
}
String postal_code = null;
String street_address = null;
String country = null;
try {
String status = jsonObject.getString("status").toString();
if (status.equalsIgnoreCase("OK")) {
JSONArray results = jsonObject.getJSONArray("results");
JSONObject r = results.getJSONObject(0);
JSONArray addressComponentsArray = r
.getJSONArray("address_components");
JSONObject geometry = r.getJSONObject("geometry");
JSONObject locationObj = geometry.getJSONObject("location");
coord = new LatLng(locationObj.getDouble("lat"), locationObj.getDouble("lng"));
}
} catch (JSONException e) {
e.printStackTrace();
}
GoogMapHandler gmh = new GoogMapHandler(fragmentManager, applicationContext);
gmh.initilizeMap(coord, "Username", "addressInfo");
}
}
答案 0 :(得分:1)
我忘记在我的ProfileActivity
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(username);
setContentView(textView);
删除它,解决了我的问题。
答案 1 :(得分:0)
我无法发表评论
你确定你的代码进入了IF块:
public void initilizeMap(LatLng coord, String username, String addrInfo) {
if (googleMap == null) {
//are you getting upto here?
....
}
}
如果在您的布局中声明了SupportMapFragment,我不认为分离会在这里做任何有意义的事情。以下应该有效:
public void initilizeMap(LatLng coord, String username, String addrInfo) {
if (googleMap == null) {
Fragment fragment = this.fragmentManager.findFragmentById(R.id.map);
SupportMapFragment supportmapfragment = (SupportMapFragment) fragment;
googleMap = supportmapfragment.getMap();
if (googleMap == null) {
Toast.makeText(applicationContext,
"Sorry! Unable to create map.", Toast.LENGTH_SHORT)
.show();
} else {
Marker loc = googleMap.addMarker(new MarkerOptions()
.position(coord).title("User: " + username)
.snippet(addrInfo));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(coord, 15));
}
} else {
Marker loc = googleMap.addMarker(new MarkerOptions()
.position(coord).title("User: " + username)
.snippet(addrInfo));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(coord, 15));
}
如果上述方法不起作用,则问题不太可能出现在代码块中,而是在映射准备好之前可能会调用它。
请给我们更多信息;告诉我们AsyncTask是否可能在调用onResume()
之前返回结果。